Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Date object

JavaScript Date object


Date object

The Date object is used to work with 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); // 参数为毫秒
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

For a more complete and practical date tutorial, 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 from a Date object as a four-digit number.
getHours() Returns the hour of the Date object (0 ~ 23).
getMilliseconds() Returns the milliseconds of the Date object (0 ~ 999).
getMinutes() Returns the minutes of the Date object (0 ~ 59).
getMonth() Returns the month from a Date object (0 ~ 11).
getSeconds() Returns the seconds of the Date object (0 ~ 59).
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 of the Date object (0 ~ 23), according to universal time.
getUTCMilliseconds() Returns the milliseconds of the Date object (0 ~ 999), according to universal time.
getUTCMinutes() Returns the minutes of the Date object (0 ~ 59), according to universal time.
getUTCMonth() Returns the month from a Date object (0 ~ 11), according to universal time.
getUTCSeconds() Returns the seconds of the Date object (0 ~ 59), according to universal time.
getYear() Deprecated. Please use the getFullYear() method instead.
parse() Returns the number of milliseconds from midnight 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 the 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 the date as a string using the ISO standard.
toJSON() Returns the date string in JSON data format.
toLocaleDateString() Converts the date portion of a Date object to a string, according to local time formatting.
toLocaleTimeString() Converts the time portion of a Date object to a string, according to local time formatting.
toLocaleString() Converts a Date object to a string, according to local time formatting.
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 the Date object.
now() Returns the number of milliseconds of the current time.