The Date object provides a variety of methods for working with dates. Let’s look at some of the most useful ones.
Creation Methods
Method
Description
Date.now()
Returns the number of milliseconds elapsed since January 1, 1970
Date.parse()
Parses a string representing a date and returns the number of milliseconds since 1970
Date.UTC()
Creates a date in UTC based on the provided arguments (year, month, day, etc.)
const currentDate = new Date();const milliseconds = Date.now();const parsedDate = Date.parse("2024-08-14T12:00:00Z");const utcDate = new Date(Date.UTC(2024, 7, 14, 12, 0, 0));
Getting Date Components
Method
Description
getDate()
Returns the day of the month (1-31) according to local date
getDay()
Returns the day of the week (0-6) according to local date
getFullYear()
Returns the full year (4 digits) according to local date
getMonth()
Returns the month (0-11) according to local date
getHours()
Returns the hour (0-23) according to local date
getMinutes()
Returns the minutes (0-59) according to local date
getSeconds()
Returns the seconds (0-59) according to local date
getMilliseconds()
Returns the milliseconds (0-999) according to local date
getTime()
Returns the number of milliseconds since January 1, 1970
valueOf()
Does the same as getTime()
getTimezoneOffset()
Returns the difference in minutes between UTC and local time
For example,
const date = new Date();console.log(date.getFullYear()); // Current yearconsole.log(date.getMonth()); // Current month (0-11)console.log(date.getDate()); // Day of the monthconsole.log(date.getDay()); // Day of the weekconsole.log(date.getHours()); // Hourconsole.log(date.getMinutes()); // Minutesconsole.log(date.getSeconds()); // Seconds
Getting Components in UTC
These are the same as the previous ones but return the date in UTC.
Method
Description
getUTCDate()
Returns the day of the month (1-31) according to UTC.
getUTCDay()
Returns the day of the week (0-6) according to UTC.
getUTCFullYear()
Returns the full year (4 digits) according to UTC.
getUTCMonth()
Returns the month (0-11) according to UTC.
getUTCHours()
Returns the hour (0-23) according to UTC.
getUTCMinutes()
Returns the minutes (0-59) according to UTC.
getUTCSeconds()
Returns the seconds (0-59) according to UTC.
getUTCMilliseconds()
Returns the milliseconds (0-999) according to UTC.
Setting Components
Method
Description
setDate()
Sets the day of the month (1-31) according to local date
setFullYear()
Sets the full year (4 digits) according to local date
setMonth()
Sets the month (0-11) according to local date
setHours()
Sets the hour (0-23) according to local date
setMinutes()
Sets the minutes (0-59) according to local date
setSeconds()
Sets the seconds (0-59) according to local date
setMilliseconds()
Sets the milliseconds (0-999) according to local date
setTime()
Sets the date and time based on a value in milliseconds since 1970.
For example,
const date = new Date();console.log(date.getFullYear()); // E.g: 2024console.log(date.getMonth()); // E.g: 7 (August, months start at 0)console.log(date.getDate()); // E.g: 14 (Day of the month)console.log(date.getDay()); // E.g: 3 (Wednesday, 0 = Sunday)console.log(date.getHours()); // E.g: 12 (Hour)console.log(date.getMinutes()); // E.g: 30 (Minutes)console.log(date.getSeconds()); // E.g: 45 (Seconds)console.log(date.getMilliseconds());// E.g: 500 (Milliseconds)console.log(date.getTime()); // Milliseconds since 1970console.log(date.getTimezoneOffset()); // E.g: -120 (Difference in minutes from UTC)
UTC Setting Methods
Method
Description
setUTCDate()
Sets the day of the month (1-31) according to UTC.
setUTCFullYear()
Sets the full year (4 digits) according to UTC.
setUTCMonth()
Sets the month (0-11) according to UTC.
setUTCHours()
Sets the hour (0-23) according to UTC.
setUTCMinutes()
Sets the minutes (0-59) according to UTC.
setUTCSeconds()
Sets the seconds (0-59) according to UTC.
setUTCMilliseconds()
Sets the milliseconds (0-999) according to UTC.
Formatting Methods
Method
Description
toDateString()
Returns the date part as a readable string
toTimeString()
Returns the time part as a readable string
toLocaleDateString()
Returns the date formatted according to the locale settings
toLocaleTimeString()
Returns the time formatted according to the locale settings
toISOString()
Returns the date in ISO 8601 format (UTC)
toUTCString()
Returns the date as a UTC string
toJSON()
Returns a JSON formatted representation of the date
For example,
const date = new Date();console.log(date.toDateString()); // "Mon Aug 14 2024"console.log(date.toTimeString()); // "12:00:00 GMT+0000 (Coordinated Universal Time)"console.log(date.toISOString()); // "2024-08-14T12:00:00.000Z"console.log(date.toLocaleDateString()); // "14/8/2024" (depending on local settings)