The date reference type in JavaScript is based on Java's language format and it stores dates in a form of milliseconds passed since midnight on January 1, 1970 UTC (Universal Time Code).

The Date object is created as a constructor, as shown here:

var currentDate = new Date();

The example above will show the current date and time. To modify different date and time to a variable, methods parse() and UTC() may be used.

Date.parse()

This method accepts a string argument that represents a date which will be converted into milliseconds. Following example shows a possible application:

var myDate = new Date(Date.parse("July 1, 2013"));

The format given inside the brackets depends heavily on the locale settings and there is a chance that the conversion fails. In such a case the result will be NaN.

This method is a default conversion method so the example above would have given same results if written like this:

var myDate = new Date("July 1, 2013");

Date.UTC()

The UTC() method also returns the date as milliseconds, but does so with different information passed as argument(s). It actually receives arguments as numbers, starting with the year as the first argument, followed by month (0 = January, 1 = February...), day (1 through 31), hour (0 - 23), minute, second and millisecond. The first two arguments (year and month) are REQUIRED. Here is an example:

// January 1st, 2013, at 1:59:59 PM

var myDate = new Date(Date.UTC(2013, 0, 1, 13, 59, 59));

If the above example would be changed to:

var myDate = new Date(Date.UTC(2013, 0));

The result would be "January 1st, 2013 at 0:01:01 (Midnight)".

If the above examples would have been written without using the Date.UTC() method, the result would be the same except that it would be considered to be in the local time zone (not UTC) as determined by the system settings.

 

›› go to examples ››