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.
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.
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.
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.
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)
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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).
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.
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.
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).
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 = newDate ();
document.write (now.toDateString ());
var now = newDate ();
var hour = now.getHours ();
var min = now.getMinutes ();
var sec = now.getSeconds ();
document.write (hour + ":" + min + ":" + sec);
<head><scripttype="text/javascript">function UpdateClock () {
var clock = document.getElementById ("clock");
var now = newDate ();
clock.innerHTML = now.toLocaleTimeString ();
}
function Init () {
UpdateClock ();
setInterval (UpdateClock, 1000);
}
</script></head><bodyonload="Init ()">
The current time is <spanid="clock"></span></body>
How to calculate the elapsed time between the selected start and end time:
<head><scripttype="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 = newDate (); // the current date and time, in local time.
var end = newDate (); // 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 = newOption ((i < 10 ? "0": "") + i, "" + i);
startHSelect.options.add (option);
var option = newOption ((i < 10 ? "0": "") + i, "" + i);
endHSelect.options.add (option);
}
for (var i = 0; i < 60; i++) {
var option = newOption ((i < 10 ? "0": "") + i, "" + i);
startMSelect.options.add (option);
var option = newOption ((i < 10 ? "0": "") + i, "" + i);
endMSelect.options.add (option);
}
CalculateElapsedTime ();
}
</script></head><bodyonload="Init ()">
h:
<selectid="starttimehour"onchange="CalculateElapsedTime ()"></select>
m:
<selectid="starttimemin"onchange="CalculateElapsedTime ()"></select>
-
h:
<selectid="endtimehour"onchange="CalculateElapsedTime ()"></select>
m:
<selectid="endtimemin"onchange="CalculateElapsedTime ()"></select><br/><br/>
Elapsed time in mins: <spanid="elapsed"></span></body>