Javascript Dates
The Date object provides us with all of what we need in order to be able to easily manipulate dates and times within our JavaScript code. If you have looked at someone else's JavaScript code and seen that they include calculations that need to keep track of how many days that there are in each month or where they have written their own code to increment the hours and zero the minutes at the end of each hour then you have found code that has been written by someone who doesn't know how to use the Date object properly because the Date object can handle all of the date and time processing for you without your having to write your own code to do it.
The first thing that we need to do to be able to manipulate dates and/or times is to create a Date object.
2 var thatDay = new Date(year, month, day, hour, minute, second, milliseconds);
3 var janone1970 = new Date(0);
4 var myDate = new Date(textdate);
- 1 sets up a date object named today and sets it to the current date and time from the computer's system clock. The parentheses are optional when setting the date object this way.
- 2 creates thatDate containing the date and time specified by the numbers passed to it. Note that you can omit numbers from the end and they will be assumed to be zero. Also the months that Date objects work with are numbered 0 through 11 rather than 1 through 12 (this is so that you can easily create an array of month names and use the month numbers to directly reference the entries from the array without having to add a blank entry to the front of the array.
Note that you are not limited to the expected number ranges when you pass values in to create a date like this. If your day number contains 54 then the appropriate date in the following month corresponding to 54 days from the start of the specified month will be set. You don't need to work out the number of days in the month and adjust the day and month fields yourself as the date object will handle it for you. - 3 if you only specify one number when creating a Date then that number will be taken to be the number of milliseconds from midnight on the first of January 1970 (which is the internal format that JavaScript uses to store dates and times).
- 4 you can use a text string to create a date object as long as the text string contains information that JavaScript is able to translate into a date (for example '1 January 2008' or 'Jul 9, 2009') Specifying values in mm/dd/ccyy format or dd/mm/ccyy format are also accepted but where both the day and month are between 1 and 11 the interpretation of exactly which date you are setting will depend on the locale settings of the browser and will therefore be under your visitor's control as to which is the intended date.
Date objects are slightly different from the other types of objects you will work with in JavaScript because they do not have any properties that can be accessed from within your JavaScript code. All of the accesses that you make will be via the various methods that dates provide. Many of these are paired and allow you to set or get the individual parts of the date and time.
today.setDate(num)
retrieve or set the day of the month
today.setMonth(num)
retrieve or set the month of the year (numbered one less than the usual month numbering since the value is expected to be used to reference an array of month names so as to retrieve that name).
today.setFullYear(num)
retrieve or set the four digit year
today.setHour(num)
retrieve or set the hour of the day (numbered between 0 and 23)
today.setMinute(num)
retrieve or set the minute within the hour (numbered between 0 and 59)
today.setSecond(num)
retrieve or set the second within the minute (also numbered 0 to 59)
today,setMilliSecond(num)
retrieve or set thousandths of a second, the smallest units of time that JavaScript can process.
Note that there is a second set of methods identical to those listed above apart from having UTC between the get/set and the unit name (eg. today.getUTCDate()). These methods work exactly the same as the above methods except that while the above methods work based on the current timezone used on the computer where the browser is running, the UTC versions apply the timezone offset in order to determine the equivalent Universal Coordinated Time (also known as GMT or z time).
There are also a number of additional methods that can be used to retrieve date/time information in other formats.
today.getUTCDay()
retrieve the day of the week for the local timezone and UTC respectively.
today.UTC()
retrieve the number of milliseconds since midnight on 1st January 1970 for the local timezone and UTC respectively.
retrieve the number of minutes difference between local and UTC time. Note that since this is a fixed value we don't need to reference any specific date object to get it but can instead reference the base Date object itself (this is known as a static method call).
another static method call, this one converts any text string that can be interpreted as a date into the corresponding number of milliseconds since 1st January 1970
extract the date and time in a locale specific format
extract the date in a locale specific format
extract the time in a locale specific format
extract the date and time in a browser specific format
extract the date in a browser specific format
extract the time in a browser specific format
today.toUTCString()
extract the UTC date and time in a browser specific format and label it as either GMT or UTC
extract the number of milliseconds since 1st January 1970


