JavaScript Date
Object
Date Object
The Date object is used to handle dates and times.
Creating a Date object: new Date()
The following four methods can also create a Date object:
var d = new Date();
var d = new Date(milliseconds); // Parameter is milliseconds
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
-
The milliseconds
parameter is a Unix timestamp, which is an integer value representing the number of milliseconds since January 1, 1970, 00:00:00 UTC (the Unix epoch).
-
The dateString
parameter represents a date as a string value.
-
year, month, day, hours, minutes, seconds, milliseconds
represent the year, month, day, hour, minute, second, and millisecond, respectively.
For a more complete date and time tutorial, refer to the JavaScript Date Object Tutorial.
Date Object Properties
Property | Description |
---|---|
constructor | Returns a reference to the Date function that created the object. |
prototype | Allows you to add properties and methods to the object. |
Date Object Methods
Method | Description |
---|---|
getDate() | Returns the day of the month (1 ~ 31) from the Date object. |
getDay() | Returns the day of the week (0 ~ 6) from the Date object. |
getFullYear() | Returns the year in four digits from the Date object. |
getHours() | Returns the hour (0 ~ 23) from the Date object. |
getMilliseconds() | Returns the milliseconds (0 ~ 999) from the Date object. |
getMinutes() | Returns the minutes (0 ~ 59) from the Date object. |
getMonth() | Returns the month (0 ~ 11) from the Date object. |
getSeconds() | Returns the seconds (0 ~ 59) from the Date object. |
getTime() | Returns the number of milliseconds since January 1, 1970. |
getTimezoneOffset() | Returns the difference in minutes between local time and Greenwich Mean Time (GMT). |
getUTCDate() | Returns the day of the month (1 ~ 31) from the Date object according to universal time. |
getUTCDay() | Returns the day of the week (0 ~ 6) from the Date object according to universal time. |
getUTCFullYear() | Returns the year in four digits from the Date object according to universal time. |
getUTCHours() | Returns the hour (0 ~ 23) from the Date object according to universal time. |
getUTCMilliseconds() | Returns the milliseconds (0 ~ 999) from the Date object according to universal time. |
getUTCMinutes() | Returns the minutes (0 ~ 59) from the Date object according to universal time. |
getUTCMonth() | Returns the month (0 ~ 11) from the Date object according to universal time. |
getUTCSeconds() | Returns the seconds (0 ~ 59) from the Date object according to universal time. |
getYear() | Deprecated. Use getFullYear() method instead. |
parse() | Returns the number of milliseconds from January 1, 1970, to the specified date (string). |
setDate() | Sets the day of the month (1 ~ 31) in the Date object. |
setFullYear() | Sets the year in the Date object with four digits. |
setHours() | Sets the hours (0 ~ 23) in the Date object. |
setMilliseconds() | Sets the milliseconds (0 ~ 999) in the Date object. |
setMinutes() | Sets the minutes (0 ~ 59) in the Date object. |
setMonth() | Sets the month (0 ~ 11) in the Date object. |
setSeconds() | Sets the seconds (0 ~ 59) in the Date object. |
setTime() | Sets the Date object in milliseconds. |
setUTCDate() | Sets the day of the month (1 ~ 31) in the Date object according to universal time. |
setUTCFullYear() | Sets the year in the Date object with four digits according to universal time. |
setUTCHours() | Sets the hours (0 ~ 23) in the Date object according to universal time. |
setUTCMilliseconds() | Sets the milliseconds (0 ~ 999) in the Date object according to universal time. |
setUTCMinutes() | Sets the minutes (0 ~ 59) in the Date object according to universal time. |
setUTCMonth() | Sets the month (0 ~ 11) in the Date object according to universal time. |
setUTCSeconds() | Sets the seconds (0 ~ 59) in the Date object according to universal time. |
setYear() | Deprecated. Use setFullYear() method instead. |
toDateString() | Converts the date portion of a Date object into a string. |
toGMTString() | Deprecated. Use toUTCString() method instead. |
toISOString() | Returns the date as a string, using the ISO standard. |
toJSON() | Returns the date as a string, formatted as a JSON date. |
toLocaleDateString() | Converts the date portion of a Date object to a string, using locale conventions. |
toLocaleTimeString() | Converts the time portion of a Date object to a string, using locale conventions. |
toLocaleString() | Converts a Date object to a string, using locale conventions. |
toString() | Converts a Date object to a string. |
toTimeString() | Converts the time portion of a Date object to a string. |
toUTCString() | Converts a Date object to a string, according to universal time. Example: var today = new Date();<br>var UTCstring = today.toUTCString(); |
UTC() | Returns the number of milliseconds since January 1, 1970, to the specified date according to universal time. |
valueOf() | Returns the primitive value of a Date object. |