Date object
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]]]]]);
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"
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.
(*) - The method is inherited from the Date.prototype.
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 () |
|
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.
|
||||||||||||||
setFullYear (numYear [, numMonth [, numDay]])* |
Sets the year, and if given, the day and month in the current Date object according to local time.
|
||||||||||||||
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.
|
||||||||||||||
setMilliseconds (numMillisec)* |
Sets the milliseconds in the current Date object according to local time.
|
||||||||||||||
setMinutes (numMin [, numSec [, numMillisec]])* |
Sets the minutes, and if given, the seconds and milliseconds in the current Date object according to local time.
|
||||||||||||||
setMonth (numMonth [, numDay])* |
Sets the month, and if given, the day in the current Date object according to local time.
|
||||||||||||||
setSeconds (numSec [, numMillisec])* |
Sets the seconds, and if given, the milliseconds in the current Date object according to local time.
|
||||||||||||||
setTime (numMillisec)* |
Sets the elapsed time in milliseconds since 01 January, 1970 00:00:00 UTC in the current Date object.
|
||||||||||||||
setUTCDate (numDay)* |
Sets the day of month in the current Date object according to universal time.
|
||||||||||||||
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.
|
||||||||||||||
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.
|
||||||||||||||
setUTCMilliseconds (numMillisec)* |
Sets the milliseconds in the current Date object according to universal time.
|
||||||||||||||
setUTCMinutes (numMin [, numSec [, numMillisec]])* |
Sets the minutes, and if given, the seconds and milliseconds in the current Date object according to universal time.
|
||||||||||||||
setUTCMonth (numMonth [, numDay])* |
Sets the month, and if given, the day in the current Date object according to universal time.
|
||||||||||||||
setUTCSeconds (numSec [, numMillisec])* |
Sets the seconds, and if given, the milliseconds in the current Date object according to universal time.
|
||||||||||||||
setYear (numYear)* |
Sets the year in the current Date object according to local time.
This method is deprecated, use the setFullYear method instead.
|
||||||||||||||
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 ()* |
|
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:
Example 2:
How to get the current date in digits:
Example 3:
How to display the current date in different formats:
Example 4:
How to get the current time:
Example 5:
How to create a date:
Example 6:
How to get the date of tomorrow:
Example 7:
How to get the date of yesterday:
Example 8:
How to get the current date and time in universal time:
Example 9:
How to get the current localized time:
Example 10:
How to get the time one hour before:
Example 11:
How to create a clock:
Example 12:
How to calculate the elapsed time between the selected start and end time:
Example 13:
How to add a new method to the Date object:
|
External links:
User Contributed Comments