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.

1 var today = new Date();
2 var thatDay = new Date(year, month, day, hour, minute, second, milliseconds);
3 var janone1970 = new Date(0);
4 var myDate = new Date(textdate);

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.


retrieve or set the day of the month


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).


retrieve or set the four digit year


retrieve or set the hour of the day (numbered between 0 and 23)


retrieve or set the minute within the hour (numbered between 0 and 59)


retrieve or set the second within the minute (also numbered 0 to 59)


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.


retrieve the day of the week for the local timezone and UTC respectively.


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


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


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow