The Date
object provides a variety of methods for working with dates. Let’s take a look at some of the most useful ones.
Creation Methods
Method | Description |
---|
Date.now() | Returns the number of milliseconds since January 1, 1970 |
Date.parse() | Parses a string representation of 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 time |
getDay() | Returns the day of the week (0-6) according to local time |
getFullYear() | Returns the full year (4 digits) according to local time |
getMonth() | Returns the month (0-11) according to local time |
getHours() | Returns the hour (0-23) according to local time |
getMinutes() | Returns the minutes (0-59) according to local time |
getSeconds() | Returns the seconds (0-59) according to local time |
getMilliseconds() | Returns the milliseconds (0-999) according to local time |
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 year
console.log(date.getMonth()); // Current month (0-11)
console.log(date.getDate()); // Day of the month
console.log(date.getDay()); // Day of the week
console.log(date.getHours()); // Hour
console.log(date.getMinutes()); // Minutes
console.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 time |
setFullYear() | Sets the full year (4 digits) according to local time |
setMonth() | Sets the month (0-11) according to local time |
setHours() | Sets the hour (0-23) according to local time |
setMinutes() | Sets the minutes (0-59) according to local time |
setSeconds() | Sets the seconds (0-59) according to local time |
setMilliseconds() | Sets the milliseconds (0-999) according to local time |
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.: 2024
console.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 1970
console.log(date.getTimezoneOffset()); // E.g.: -120 (Difference in minutes with UTC)
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. |
Method | Description |
---|
toDateString() | Returns the date part as a readable string |
toTimeString() | Returns the time part as a readable string |
toLocaleDateString() | Returns the formatted date according to locale settings |
toLocaleTimeString() | Returns the formatted time according to locale settings |
toISOString() | Returns the date in ISO 8601 format (UTC) |
toUTCString() | Returns the date as a UTC string |
toJSON() | Returns a JSON format 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)