Another extremely useful type of object to use in your JavaScript are those derived from the Date object. Date objects provide a way to store a particular date/time where the methods available to date objects allow for easy calculations involving dates and times without your having to manually code everything. With a date object if you add a number of minutes or days to the object that will take the time or date past the end of the current hour or month the calculation of the correct time and date is done for you automatically without your having to worry about how many minutes there are in an hour, how many days there are in a month, or anything else regarding the translation between the different possible parts that make up a date and time.
Unlike with objects, arrays, numbers, and strings, there is no shortcut way to define a date. There are a number of different ways that you can pass information regarding the date and time you are initialising the Date object to though.
var today = new Date();
var someday = new Date(millisecondsSince1970);
var anotherDate = new Date(y, m, d, h, m, s, ms);
When you don't pass anything in creating the Date it defaults to the current date and time.
All dates are stored internally as the number of milliseconds since midnight 1st January 1970 UTC (Universal Coordinated Time - previosly called GMT) and so you can pass in a single number representing that value. You can attempt to generate a date time in that format using Date.parse(dateTimeString) where if dateTimeString is in a format that can be recognised as a valid date and time it will return the milliseconds number that the Date object expects. If it doesn't contain a recognisable date/time it will return NaN instead.
The third way to define a date involves passing in the year, month, day, hour, minute, second, and millisecond values as separate parameters. You must include the year and month for this to work (as otherwise it assumes that the single number supplied is milliseconds since 1970). You can supply as many or few of the rest as you need. It doesn't matter if the values are out of the regular range for that field as if you specified 65 say for the number of minutes it would simply assume that it was five minutes into the hour after the one supplied.
Once you have created a date object you can set any of the component parts of the date/time by using one of the set methods - setDate(), setMonth(), setFullYear, setHours(), setMinutes(), setSeconds(), and setMilliseconds(). Each of those will update the value stored based on the local date/time from the system clock on the computer where the browser is running. You can also make use of the timezone offset info on that computer to setUTCDate() ryc based on the UTC timezone.
There are get methods corresponding to all of the above set methods that can retrieve those parts of the date/time. You can therefore easily update any part of the date time by combining the get and set methods.
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate()+1);
Additional get methods exists getDay() which retrieves the day of the week as a number between 0 (Sunday) and 6 (Saturday) and getTimeZoneOffset() which returns the number of minutes difference between the local and UTC timezones, as well as toDateString() and toTimeString() which retrieve the date and time parts of the date in an appropriate local format.
One thing in particular to watch for when retrieving dates to display them in your web page is to remember that not everyone uses the dd/mm/ccyy format for dates and if you display dates in a format that is different from what your visitors expect then they may end up wondering why you are referencing 6th January when they thought you should be referencing the 1st June.
More of This Tutorial
- 1 Hello World
- 2 Events
- 3 Visitor Triggered Events
- 4 Timed Events
- 5 Testing Conditions
- 6 Feature Sensing
- 7 Alternate Tag Location
- 8 Functions and Methods
- 9 Passing Parameters
- 10 Variables/Properties
- 11 Objects
- 12 Arrays
- 13 Nodelists
- 14 Loops
- 15 Numbers
- 16 Strings
- 18 Alert
- 19 Other Objects
- 20 Extending Objects
- 21 Creating Objects
- 22 Collision Proofing
- 23 JavaScript and Forms
- 24 Updating the Web Page
- 25 Obsolete JavaScript

