You are here: Reference > JavaScript > core > objects > Date

Date object

Browser support:
The Date object allows the handling of date and time.
It supports methods to get or set the date and time in GMT (Greenwich Mean Time), in local time or in an arbitrary time zone.

Syntax:

Creating a new Date Object:
var date = new Date ();
Creating a Date object for a specific date or time:
new Date (milliseconds);
new Date (dateString);
new Date (year, month[, date[, h[, m[, s[, ms]]]]]);
  • If the Date object is created without arguments, then it contains the current date and time, in local time.
  • If the Date object is created with milliseconds, it is measured from midnight 01 January, 1970 UTC.
  • If the Date object is created with a dateString, then the Date object tries to parse the given string first.
    The accepted date formats are different in different browsers.
    Some of the formats that are supported by Internet Explorer, Firefox, Opera, Google Chrome and Safari:
    • "5/18/1981"
    • "5/18/1981 11:23:48"
    • "5/18/1981 GMT+0130"
    • "5/18/1981 11:23:48 GMT+0130"
    • "18 May 1981"
    • "18 May 1981 11:23:48"
    • "18 May 1981 GMT+0130"
    • "18 May 1981 11:23:48 GMT+0130"
    • "Mon, 5/18/1981"
    • "Mon, 5/18/1981 11:23:48"
    • "Mon, 5/18/1981 GMT+0130"
    • "Mon, 5/18/1981 11:23:48 GMT+0130"
    • "Mon, 18 May 1981"
    • "Mon, 18 May 1981 11:23:48"
    • "Mon, 18 May 1981 GMT+0130"
    • "Mon, 18 May 1981 11:23:48 GMT+0130"
    Note that the accepted date formats are the same as those that the parse method supports.
    If the date format does not contain time zone information (GMT+0130), then the local time zone is used.

    If you need more information about the supported date formats, visit these sites:
    Date.parse method (MSDN)
    Date.parse method (Mozilla)
    • You can create a Date object by specifying the year, month, day, hours, minutes, seconds and milliseconds values as integers.
    • The parameters specify the date and time in local time.
    • The year and month parameters are required, the others are optional.
    • Use full years (1981 not 81) to avoid the Y2K problem.
    • Months are indexed from zero (0:January, 11:December).
    • If the date parameter is not specified, a value of 1 will be used.
    • The default value is 0 for the other parameters.

Members:

The Date object inherits from the Date.prototype and Function.prototype objects. The following lists only contain the members of the Date and Date.prototype objects.

Properties:

Property Support Description
prototype
Returns a reference to the Date.prototype object. The Date.prototype object allows adding properties and methods to the Date object that can be used with instances of the Date object, like any predefined property or method. The prototype property is static, it cannot be accessed from an instance of the Date object, only Date.prototype is allowed.

Methods:

Method Support Description
getDate ()*
Returns an integer that represents the day of month in the current Date object according to local time. Returned value: 1 - 31.
getDay ()*
Returns an integer that represents the day of week in the current Date object according to local time. Returned value: 0 - 6. (Sunday = 0)
getFullYear ()*
Returns an integer that represents the full year (e.g. 1984) in the current Date object according to local time.
getHours ()*
Returns an integer that represents the hours (in 24-hour time) in the current Date object according to local time. Returned value: 0 - 23.
getMilliseconds ()*
Returns an integer that represents the milliseconds in the current Date object according to local time. Returned value: 0 - 999.
getMinutes ()*
Returns an integer that represents the minutes in the current Date object according to local time. Returned value: 0 - 59.
getMonth ()*
Returns an integer that represents the month in the current Date object according to local time. Returned value: 0 - 11. (January = 0)
getSeconds ()*
Returns an integer that represents the seconds in the current Date object according to local time. Returned value: 0 - 59.
getTime ()*
Returns an integer that represents the milliseconds elapsed since 01 January, 1970 00:00:00 UTC to the time value in the current Date object according to local time.
getTimezoneOffset ()*
Returns an integer that represents the distance in minutes between the local time and the UTC (Universal Coordinated Time).
getUTCDate ()*
Returns an integer that represents the day of month in the current Date object according to standard time (UTC). Returned value: 1 - 31.
getUTCDay ()*
Returns an integer that represents the day of week in the current Date object according to standard time (UTC). Returned value: 0 - 6. (0 means Sunday)
getUTCFullYear ()*
Returns an integer that represents the full year (e.g. 1984) in the current Date object according to standard time (UTC).
getUTCHours ()*
Returns an integer that represents the hours (in 24-hour time) in the current Date object according to standard time (UTC). Returned value: 0 - 23.
getUTCMilliseconds ()*
Returns an integer that represents the milliseconds in the current Date object according to standard time (UTC). Returned value: 0 - 999.
getUTCMinutes ()*
Returns an integer that represents the minutes in the current Date object according to standard time (UTC). Returned value: 0 - 59.
getUTCMonth ()*
Returns an integer that represents the month in the current Date object according to standard time (UTC). Returned value: 0 - 11. (January = 0)
getUTCSeconds ()*
Returns an integer that represents the seconds in the current Date object according to standard time (UTC). Returned value: 0 - 59.
getVarDate ()*
Returns the value of the Date object in VT_DATE format (Sun Oct 12 02:11:11 UTC+0100 2008).
getYear ()*
Returns an integer that represents the year in the current Date object according to local time. The returned value represents the full year (e.g. 1984), in Internet Explorer and Opera, and contains the years elapsed since 1900, in Firefox, Google Chrome and Safari.
now ()
10.5
Returns an integer that represents the milliseconds elapsed since 01 January, 1970 00:00:00 UTC to the current time. The now method is static, it cannot be accessed from an instance of the Date object, only Date.now is allowed. For a cross-browser solution, use the getTime method instead.
parse (dateString)
Parses the dateString parameter and returns an integer that represents the milliseconds elapsed since 01 January, 1970 00:00:00 UTC according to the parsed date. You can find a description about the accepted date formats at the top of this page in the 'Syntax' section. The parse method is static, it cannot be accessed from an instance of the Date object, only Date.parse is allowed.
setDate (numDay)*
Sets the day of month in the current Date object according to local time.
numDay Required. An integer that specifies the day (1 means the first day in the month).
Negative and large values can also be used for the numDay parameter. For example, when the month in the current Date object is May and the value of the numDay parameter is 32, then it means June 1. If the value of the numDay parameter is 0, then it means April 30.
setFullYear (numYear [, numMonth [, numDay]])*
Sets the year, and if given, the day and month in the current Date object according to local time.
numYear Required. An integer that specifies the year. Use full years (e.g. 1984).
numMonth Optional. An integer that specifies the month (0 means January ... 11 means December).
numDay Optional. An integer that specifies the day (1 means the first day in the month).
Negative and large values can also be used for the numMonth and numDay parameters. For example, when the value of numYear is 2002 and the value of numMonth is 12, it means 2003 January. If the value of numMonth is -1, it means 2001 December.
setHours (numHour [, numMin [, numSec [, numMillisec]]])*
Sets the hours, and if given, the minutes, seconds and milliseconds in the current Date object according to local time.
numHour Required. An integer that specifies the hour (from 0 to 23).
numMin Optional. An integer that specifies the minutes (from 0 to 59).
numSec Optional. An integer that specifies the seconds (from 0 to 59).
numMillisec Optional. An integer that specifies the milliseconds (from 0 to 999).
Negative and large values can also be used for the parameters. For example, when the value of numHour is 10 and the value of numMin is 62, it means 11:02. If the value of numMin is -1, it means 9:59.
setMilliseconds (numMillisec)*
Sets the milliseconds in the current Date object according to local time.
numMillisec Required. An integer that specifies the milliseconds (from 0 to 999).
Negative and large values can also be used for the numMillisec parameter. For example, when the time in the current Date object is 9:10:23 and the value of the numMillisec parameter is 1002, then it means 9:10:24:002. If the value of the numMillisec parameter is -1, then it means 9:10:22:999.
setMinutes (numMin [, numSec [, numMillisec]])*
Sets the minutes, and if given, the seconds and milliseconds in the current Date object according to local time.
numMin Required. An integer that specifies the minutes (from 0 to 59).
numSec Optional. An integer that specifies the seconds (from 0 to 59).
numMillisec Optional. An integer that specifies the milliseconds (from 0 to 999).
Negative and large values can also be used for the parameters. For example, when the value of numMin is 40 and the value of numSec is 62, it means 41:02. If the value of numSec is -1, it means 39:59.
setMonth (numMonth [, numDay])*
Sets the month, and if given, the day in the current Date object according to local time.
numMonth Required. An integer that specifies the month (from 0 to 11).
numDay Optional. An integer that specifies the day (from 1 to 31).
Negative and large values can also be used for the numMonth and numDay parameters. For example, when the value of numMonth is 4 (May) and the value of numDay is 32, it means June 1. If the value of numDay is 0, it means April 30.
setSeconds (numSec [, numMillisec])*
Sets the seconds, and if given, the milliseconds in the current Date object according to local time.
numSec Required. An integer that specifies the seconds (from 0 to 59).
numMillisec Optional. An integer that specifies the milliseconds (from 0 to 999).
Negative and large values can also be used for the parameters. For example, when the value of numSec is 40 and the value of numMillisec is 1002, it means 41:002. If the value of numMillisec is -1, it means 39:999.
setTime (numMillisec)*
Sets the elapsed time in milliseconds since 01 January, 1970 00:00:00 UTC in the current Date object.
numMillisec Required. An integer that specifies the milliseconds.
setUTCDate (numDay)*
Sets the day of month in the current Date object according to universal time.
numDay Required. An integer that specifies the day (form 1 to 31).
Negative and large values can also be used for the numDay parameter. For example, when the month in the current Date object is May and the value of the numDay parameter is 32, then it means June 1. If the value of the numDay parameter is 0, then it means April 30.
setUTCFullYear (numYear [, numMonth [, numDay]])*
Sets the full year (e.g. 1984), and if given, the day and month in the current Date object according to universal time.
numYear Required. An integer that specifies the year.
numMonth Optional. An integer that specifies the month (from 0 to 11).
numDay Optional. An integer that specifies the day (from 1 to 31).
Negative and large values can also be used for the numMonth and numDay parameters. For example, when the value of numYear is 2002 and the value of numMonth is 12, it means 2003 January. If the value of numMonth is -1, it means 2001 December.
setUTCHours (numHour [, numMin [, numSec [, numMillisec]]])*
Sets the hour, and if given, the minutes, seconds and milliseconds in the current Date object according to universal time.
numHour Required. An integer that specifies the hour (from 0 to 23).
numMin Optional. An integer that specifies the minutes (from 0 to 59).
numSec Optional. An integer that specifies the seconds (from 0 to 59).
numMillisec Optional. An integer that specifies the milliseconds (from 0 to 999).
Negative and large values can also be used for the parameters. For example, when the value of numHour is 10 and the value of numMin is 62, it means 11:02. If the value of numMin is -1, it means 9:59.
setUTCMilliseconds (numMillisec)*
Sets the milliseconds in the current Date object according to universal time.
numMillisec Required. An integer that specifies the milliseconds (from 0 to 999).
Negative and large values can also be used for the numMillisec parameter. For example, when the time in the current Date object is 9:10:23 and the value of the numMillisec parameter is 1002, then it means 9:10:24:002. If the value of the numMillisec parameter is -1, then it means 9:10:22:999.
setUTCMinutes (numMin [, numSec [, numMillisec]])*
Sets the minutes, and if given, the seconds and milliseconds in the current Date object according to universal time.
numMin Required. An integer that specifies the minutes (from 0 to 59).
numSec Optional. An integer that specifies the seconds (from 0 to 59).
numMillisec Optional. An integer that specifies the milliseconds (from 0 to 999).
Negative and large values can also be used for the parameters. For example, when the value of numMin is 40 and the value of numSec is 62, it means 41:02. If the value of numSec is -1, it means 39:59.
setUTCMonth (numMonth [, numDay])*
Sets the month, and if given, the day in the current Date object according to universal time.
numMonth Required. An integer that specifies the month (from 0 to 11).
numDay Optional. An integer that specifies the day (from 1 to 31).
Negative and large values can also be used for the numMonth and numDay parameters. For example, when the value of numMonth is 4 (May) and the value of numDay is 32, it means June 1. If the value of numDay is 0, it means April 30.
setUTCSeconds (numSec [, numMillisec])*
Sets the seconds, and if given, the milliseconds in the current Date object according to universal time.
numSec Required. An integer that specifies the seconds (from 0 to 59).
numMillisec Optional. An integer that specifies the milliseconds (from 0 to 999).
Negative and large values can also be used for the parameters. For example, when the value of numSec is 40 and the value of numMillisec is 1002, it means 41:002. If the value of numMillisec is -1, it means 39:999.
setYear (numYear)*
Sets the year in the current Date object according to local time. This method is deprecated, use the setFullYear method instead.
numYear Required. An integer that specifies the year.
toDateString ()*
Returns the date portion (without time) of the current Date object as a string (like: Thu Apr 5 1984). Always returns the English name of the month and the day. If you need the locale specific names of the months and the days, use the toLocaleDateString method.
toGMTString ()*
Returns the entire contents of the current Date object in GMT (Greenwich Mean Time) time as a string.
toISOString ()*
93.510.55
Returns the entire contents of the current Date object in ISO format.
toLocaleDateString ()*
Returns the date portion (without time) of the current Date object as a localized string. The returned string contains the locale specific (English, German, Japanese ...) name of the month and the day.
toLocaleString ()*
Returns the entire contents of the current Date object as a localized string. The returned string contains the month and the day names in the language that is used by the default locale of the operating system.
toLocaleTimeString ()*
Returns the time portion (without date) of the current Date object as a localized string. The returned string is dependent on the operating system's locale, hich means that the returned format can be different (e.g. 24-hour or 12-hour time).
toSource ( )*
Returns a string representing the source code of the current Date object.
toString ( )*
Returns the entire contents of the current Date object in american date format (like: Mon May 18 1981 11:23:48 GMT+0200 (Romance Daylight Time)).
When a Date object needs to be converted to a string, the JavaScript interpreter automatically calls its toString method.
toTimeString ()*
Returns the time portion (without date) of the current Date object as a string. The returned time: 12:23:16 UTC+0100. If you need the locale specific time, use the toLocaleTimeString method.
toUTCString ()*
Returns the entire contents of the current Date object according to universal time as a string.
UTC ([year, [month, [day [, hours [, minutes [, seconds [, millisec]]]]]]])
Returns an integer that represents the milliseconds elapsed since 01 January, 1970 00:00:00 UTC according to universal time, relative to the given date. The UTC method is static, it cannot be accessed from an instance of the Date object, only Date.UTC is allowed.
year - Required. An integer that specifies the year.
month - Optional. An integer that specifies the month (from 0 to 11).
day - Optional. An integer that specifies the day (from 1 to 31). hours - Required. An integer that specifies the hour (from 0 to 23).
minutes - Optional. An integer that specifies the minutes (from 0 to 59).
seconds - Optional. An integer that specifies the seconds (from 0 to 59).
millisec - Optional. An integer that specifies the milliseconds (from 0 to 999).
valueOf ()*
Returns an integer that represents the milliseconds elapsed since 01 January, 1970 00:00:00 UTC to the time value in the current Date object according to universal time.

(*) - The method is inherited from the Date.prototype.

Examples:

Example 1:

How to get the current date as a string:
var now = new Date ();
document.write (now.toDateString ());
Did you find this example helpful? yes no

Example 2:

How to get the current date in digits:
var now = new Date ();
var year = now.getFullYear ();
    // the current month and day in digits
var monthInDigits = now.getMonth () + 1;
var dayInDigits = now.getDate ();
    // two digits values
monthInDigits = ((monthInDigits < 10) ? "0" : "") + monthInDigits;
dayInDigits = ((dayInDigits < 10) ? "0" : "") + dayInDigits;

document.write (year + "." + monthInDigits + "." + dayInDigits);
Did you find this example helpful? yes no

Example 3:

How to display the current date in different formats:
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

var now = new Date ();
var year = now.getFullYear ();
var month = months[now.getMonth ()];
var day = days[now.getDay ()];

    // the current month and day in digits
var monthInDigits = now.getMonth () + 1;
var dayInDigits = now.getDate ();
    // two digits values
monthInDigits = ((monthInDigits < 10) ? "0" : "") + monthInDigits;
dayInDigits = ((dayInDigits < 10) ? "0" : "") + dayInDigits;

document.write (year + "." + monthInDigits + "." + dayInDigits);
document.write ("<br/>");
document.write (day + ", " + year + "." + monthInDigits + "." + dayInDigits);
document.write ("<br/>");
document.write (monthInDigits + "/" + dayInDigits + "/" + year);
document.write ("<br/>");
document.write (day + ", " + monthInDigits + "/" + dayInDigits + "/" + year);
document.write ("<br/>");
document.write (dayInDigits + " " + month + " " + year);
document.write ("<br/>");
document.write (day + ", " + dayInDigits + " " + month + " " + year);
document.write ("<br/>");
Did you find this example helpful? yes no

Example 4:

How to get the current time:
var now = new Date ();
var hour = now.getHours  ();
var min = now.getMinutes  ();
var sec = now.getSeconds  ();

document.write (hour + ":" + min + ":" + sec);
Did you find this example helpful? yes no

Example 5:

How to create a date:
var date = new Date (1999, 6, 24);
document.write (date.toDateString ()); // output: Sat Jul 24 1999
Did you find this example helpful? yes no

Example 6:

How to get the date of tomorrow:
var tomorrow = new Date ();
tomorrow.setDate (tomorrow.getDate () + 1);
document.write (tomorrow.toDateString ());
Did you find this example helpful? yes no

Example 7:

How to get the date of yesterday:
var yesterday = new Date ();
yesterday.setDate (yesterday.getDate () - 1);
document.write (yesterday.toDateString ());
Did you find this example helpful? yes no

Example 8:

How to get the current date and time in universal time:
var now = new Date ();
document.write (now.toUTCString ());
Did you find this example helpful? yes no

Example 9:

How to get the current localized time:
var now = new Date ();
document.write (now.toLocaleTimeString ());
Did you find this example helpful? yes no

Example 10:

How to get the time one hour before:
var oneHourBefore = new Date ();
oneHourBefore.setHours (oneHourBefore.getHours () - 1); 
document.write (oneHourBefore.toLocaleTimeString ());
Did you find this example helpful? yes no

Example 11:

How to create a clock:
<head>
    <script type="text/javascript">
        function UpdateClock () {
            var clock = document.getElementById ("clock");
            var now = new Date ();
            clock.innerHTML = now.toLocaleTimeString ();
        }
        function Init () {
            UpdateClock ();
            setInterval (UpdateClock, 1000);
        }

    </script>
</head>
<body onload="Init ()">
    The current time is <span id="clock"></span>
</body>
Did you find this example helpful? yes no

Example 12:

How to calculate the elapsed time between the selected start and end time:
<head>
    <script type="text/javascript">
        function CalculateElapsedTime () {
            var startHSelect = document.getElementById ("starttimehour");
            var startMSelect = document.getElementById ("starttimemin");
            var endHSelect = document.getElementById ("endtimehour");
            var endMSelect = document.getElementById ("endtimemin");

                // convert string values to integers
            var startH = parseInt (startHSelect.value);
            var startM = parseInt (startMSelect.value);
            var endH = parseInt (endHSelect.value);
            var endM = parseInt (endMSelect.value);

                // create Date objects from start and end
            var start = new Date ();    // the current date and time, in local time.
            var end = new Date ();  // the current date and time, in local time.

                // set the selected hours and mins
            start.setHours (startH, startM);
            end.setHours (endH, endM);

                // calculate the elapsed time in milliseconds
            var elapsedInMS = end.getTime () - start.getTime ();

                // display the result
            var elapsedSpan = document.getElementById ("elapsed");
            elapsedSpan.innerHTML = "" + (elapsedInMS / 1000 / 60);
        }
 
        function Init () {
            var startHSelect = document.getElementById ("starttimehour");
            var startMSelect = document.getElementById ("starttimemin");
            var endHSelect = document.getElementById ("endtimehour");
            var endMSelect = document.getElementById ("endtimemin");

                // fill the selection lists 
            for (var i = 0; i < 24; i++) {
                var option = new Option ((i < 10 ? "0": "") + i, "" + i);
                startHSelect.options.add (option);
                var option = new Option ((i < 10 ? "0": "") + i, "" + i);
                endHSelect.options.add (option);
            }

            for (var i = 0; i < 60; i++) {
                var option = new Option ((i < 10 ? "0": "") + i, "" + i);
                startMSelect.options.add (option);
                var option = new Option ((i < 10 ? "0": "") + i, "" + i);
                endMSelect.options.add (option);
            }

            CalculateElapsedTime ();
        }
    </script>
</head>
<body onload="Init ()">
    h:
    <select id="starttimehour" onchange="CalculateElapsedTime ()"></select>
    m:
    <select id="starttimemin" onchange="CalculateElapsedTime ()"></select>
    -
    h:
    <select id="endtimehour" onchange="CalculateElapsedTime ()"></select>
    m:
    <select id="endtimemin" onchange="CalculateElapsedTime ()"></select>
    <br /><br />
    Elapsed time in mins: <span id="elapsed"></span>
</body>
Did you find this example helpful? yes no

Example 13:

How to add a new method to the Date object:
function NextMonth () {
    var month = this.getMonth ();
    month = (month == 11)? 0 : month + 1;
    this.setMonth (month);
}
Date.prototype.nextMonth = NextMonth;

var date = new Date ();
document.write (date.toDateString ());

document.write ("<br />");

date.nextMonth ();
document.write (date.toDateString ());
Did you find this example helpful? yes no

External links:

User Contributed Comments

Post Content

Post Content