English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Date Object

Date object

The Date object is used to handle dates and times.

The Date object is created using the new Date() constructor.

The Date object is based on a time value, which is the number of milliseconds since UTC 1970 years1Month1milliseconds since the day of the month.

You can use any of the following syntaxes to create a Date object using the Date() constructor.

new Date();new Date(milliseconds);new Date(dateString);new Date(year, month, day, hours, minutes, seconds, milliseconds);

Note: The month in JavaScript is from 0 to11to count. This means1The month is 0,12The month is11.

You can find more information in ourIn the JavaScript Date tutorialLearn more about dates and times.

Date Object Properties

The following table lists the properties of the Date object:

PropertiesDescription
constructorReturns the function that creates the prototype of the Date object
prototypeAllows you to add new properties and methods to the object

Date Object Methods

The following table lists the methods of the Date object:

MethodDescription
getDate() Returns the day of the month for the specified date object according to local time (1-31)
getDay()Returns the day of the week for the specified date object according to local time (0-6)
getFullYear()Returns the year of the specified date object according to local time (returns four-digit numbers when the year is four digits).
getHours()Returns the hours of the specified date object according to local time (0-23)
getMilliseconds()Returns the milliseconds of the specified date object according to local time (0-999)
getMinutes()Returns the minutes of the specified date object according to local time (0-59)
getMonth() Returns the month of the specified date object according to local time (0-11)
getSeconds()Returns the number of seconds since the specified date object according to local time (0-59)
getTime()Returns the number of milliseconds since UTC 1970 years1Month1Milliseconds since midnight on day
getTimezoneOffset()Returns the time difference between UTC time and local time, in minutes
getUTCDate()Returns the day of the month for a specified date according to universal time (from1-31Starting)
getUTCDay()Returns the day of the week (from 0 to6)
getUTCFullYear()Returns the year based on world time
getUTCHours()Returns the hours (from 0-23Starting)
getUTCMilliseconds()Returns the milliseconds (from 0 to999)
getUTCMinutes()Returns the minutes (from 0 to59)
getUTCMonth()Returns the month (from 0 to11)
getUTCSeconds()Returns the seconds (from 0 to59)
getYear()Not recommended for use.UsegetFullYear()Method instead
setDate()Sets the day of the month for the specified date object based on local time.
setFullYear()Sets the full year (four-digit year is four digits) for the specified date object based on local time.
setHours()Sets the hours of the date object for the specified date object based on local time.
setMilliseconds()Sets the milliseconds of the date object based on local time
setMinutes()Sets the minutes of the date object based on local time
setMonth()Sets the month of the date object based on local time
setSeconds()Sets the seconds of the date object based on local time
setTime()Sets the date to1970 years1Month1Days before/After the specified number of milliseconds
setUTCDate()Sets the day of the month of the date object based on world standard time
setUTCFullYear()Sets the year of the date object based on world standard time
setUTCHours()Sets the hour of the date object based on universal time
setUTCMilliseconds()Sets the milliseconds of the date object based on world standard time
setUTCMinutes()Sets the minutes of the date object based on universal time
setUTCMonth()Sets the month of the date object based on universal time
setUTCSeconds()Sets the seconds of the date object based on universal time
setYear()Not recommended for use.UsesetFullYear()Method instead
toDateString()In the form of human-readable (human-Returns the string representation of the date part of the date object in a human-readable (human) form.
toGMTString()Not recommended for use.UsetoUTCString()Method instead
toISOString()Returns the date as a string, formatted according to the ISO standard
toJSON()Returns the date as a string in JSON date format
toLocaleDateString()Returns the date part of the Date object as a string in local format
toLocaleTimeString()Return the time part of the Date object as a string in local format
toLocaleString()Convert the Date object to a string in local format
toString()Convert the Date object to a string
toTimeString()Convert the time part of the Date object to a string
toUTCString()Convert the Date object to a string based on universal time
valueOf()Return the original value of the Date object

Date Static Methods

The Date object also defines three static methods. These methods are called through the Date() constructor itself.

MethodDescription
Date.now()Return from world standard time1970 years1Month1Milliseconds since midnight on day
Date.parse()Parse a date string and return from1970 years1Month1Milliseconds since 00:00:00 on day (UTC), ignoring leap seconds
Date.UTC()Return from1970 years1Month1Milliseconds since Date object since 00:00:00 on day.