JavaScript DateObject
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 Date objects:
var d = new Date(); var d = new Date(milliseconds); // 参数为毫秒 var d = new Date(dateString); var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
-
millisecondsThe 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).
-
dateStringThe parameter represents a string value of a date.
-
year, month, day, hours, minutes, seconds, millisecondsRepresent year, month, day, hour, minute, second, and millisecond respectively.
For more complete dates and practical tutorials, please refer toJavaScript Date Object Tutorial。
Date Object Properties
| Property | Description |
|---|---|
| constructor | Returns a reference to the Date function that created this object. |
| prototype | Gives you the ability to add properties and methods to an object. |
Date Object Methods
| Method | Description |
|---|---|
| getDate() | Returns the day of the month (1 ~ 31) from a Date object. |
| getDay() | Returns the day of the week (0 ~ 6) from a Date object. |
| getFullYear() | Returns the year as a four-digit number from a Date object. |
| getHours() | Returns the hour (0 ~ 23) of a Date object. |
| getMilliseconds() | Returns the milliseconds (0 ~ 999) of a Date object. |
| getMinutes() | Returns the minutes (0 ~ 59) of a Date object. |
| getMonth() | Returns the month (0 ~ 11) from a Date object. |
| getSeconds() | Returns the seconds (0 ~ 59) of a 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 a Date object according to universal time. |
| getUTCDay() | Returns the day of the week (0 ~ 6) from a Date object according to universal time. |
| getUTCFullYear() | Returns the four-digit year from a Date object according to universal time. |
| getUTCHours() | Returns the hour (0 ~ 23) of a Date object according to universal time. |
| getUTCMilliseconds() | Returns the milliseconds (0 ~ 999) of a Date object according to universal time. |
| getUTCMinutes() | Returns the minutes (0 ~ 59) of a Date object according to universal time. |
| getUTCMonth() | Returns the month (0 ~ 11) from a Date object according to universal time. |
| getUTCSeconds() | Returns the seconds (0 ~ 59) of a Date object according to universal time. |
| getYear() | Deprecated.Please use the getFullYear() method instead. |
| parse() | Returns the number of milliseconds from midnight on January 1, 1970 to the specified date (string). |
| setDate() | Sets the day of the month (1 ~ 31) in a Date object. |
| setFullYear() | Sets the year (four digits) in a Date object. |
| setHours() | Sets the hour (0 ~ 23) in a Date object. |
| setMilliseconds() | Sets the milliseconds (0 ~ 999) in a Date object. |
| setMinutes() | Sets the minutes (0 ~ 59) in a Date object. |
| setMonth() | Sets the month (0 ~ 11) in a Date object. |
| setSeconds() | Sets the seconds (0 ~ 59) in a Date object. |
| setTime() | The setTime() method sets a Date object in milliseconds. |
| setUTCDate() | Sets the day of the month (1 ~ 31) in a Date object according to universal time. |
| setUTCFullYear() | Sets the year (four digits) in a Date object according to universal time. |
| setUTCHours() | Sets the hour (0 ~ 23) in a Date object according to universal time. |
| setUTCMilliseconds() | Sets the milliseconds (0 ~ 999) in a Date object according to universal time. |
| setUTCMinutes() | Sets the minutes (0 ~ 59) in a Date object according to universal time. |
| setUTCMonth() | Sets the month (0 ~ 11) in a Date object according to universal time. |
| setUTCSeconds() | The setUTCSeconds() method is used to set the seconds field of a specified time according to universal time (UTC). |
| setYear() | Deprecated.Please use the setFullYear() method instead. |
| toDateString() | Converts the date portion of a Date object to a string. |
| toGMTString() | Deprecated.Please use the toUTCString() method instead. |
| toISOString() | Returns a date format as a string using the ISO standard. |
| toJSON() | Returns a date string in JSON data format. |
| toLocaleDateString() | Converts the date portion of a Date object to a string according to the local time format. |
| toLocaleTimeString() | Converts the time portion of a Date object to a string according to the local time format. |
| toLocaleString() | Converts a Date object to a string according to the local time format. |
| 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(); var UTCstring = today.toUTCString(); |
| UTC() | Returns the number of milliseconds from January 1, 1970 to the specified date according to universal time. |
| valueOf() | Returns the primitive value of a Date object. |
| now() | Returns the number of milliseconds of the current time. |