new Date()
generates a date object representing the current date and time.
const date = new Date(); |
Method |
Description |
getFullYear() |
Retrieve the year in four-digit format (yyyy). |
getMonth() |
Retrieve the month as a number, ranging from 0 to 11. |
getDate() |
Retrieve the day as a number, ranging from 1 to 31. |
getDay() |
Retrieve the weekday as a number, ranging from 0 to 6. |
getHours() |
Retrieve the hour in the range from 0 to 23. |
getMinutes() |
Retrieve the minute within the range of 0 to 59. |
getSeconds() |
Retrieve the second within the range of 0 to 59. |
getMilliseconds() |
Retrieve the millisecond within the range of 0 to 999. |
getTime() |
Retrieve the time expressed in milliseconds since January 1, 1970. |
Note 1 The aforementioned get methods return local time. Information about Universal Time (UTC) is provided at the bottom of this page. |
Note 2 The get methods extract information from existing date objects. In a date object, the time remains static; it doesn’t change dynamically. The time within a date object does NOT represent the current time. |
The getFullYear() Method
The ‘getFullYear()
‘ method retrieves the year of a date as a four-digit number.
Example
const d = new Date(“2021-03-25”); d.getFullYear(); |
const d = new Date(); d.getFullYear(); |
Warning!Older JavaScript code may utilize the non-standard method ‘ |
The ‘getMonth()
‘ method retrieves the month of a date as a number, ranging from 0 to 11.
NoteIn JavaScript, January is represented as month number 0, February as 1, and so forth, with December being month number 11. |
Example
const d = new Date(“2021-03-25”); d.getMonth(); |
const d = new Date(); d.getMonth(); |
Note
To retrieve the month name, you can utilize an array containing the names of the months. |
Example
const months = [“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”]; |
const months = [“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”]; |
The getDate() function provides the numerical representation of the day within a date (ranging from 1 to 31).
Example
const d = new Date(“2021-03-25”); d.getDate(); |
const d = new Date(); d.getDate(); |
The getHours() function yields the numeric representation of the hour within a date (ranging from 0 to 23).
Example
const d = new Date(“2021-03-25”); d.getHours(); |
const d = new Date(); d.getHours(); |
The getMinutes() function retrieves the numerical representation of the minutes within a date (ranging from 0 to 59).
Example
const d = new Date(“2021-03-25”); d.getMinutes(); |
const d = new Date(); d.getMinutes(); |
The getSeconds() function provides the numeric representation of the seconds within a date (ranging from 0 to 59).
Examples
const d = new Date(“2021-03-25”); d.getSeconds(); |
const d = new Date(); d.getSeconds(); |
The getMilliseconds() function yields the numerical representation of the milliseconds within a date (ranging from 0 to 999).
Examples
const d = new Date(“2021-03-25”); d.getMilliseconds(); |
const d = new Date(); d.getMilliseconds(); |
The getDay() function retrieves the numerical representation of the weekday within a date (ranging from 0 to 6).
NoteIn JavaScript, Sunday is considered the first day of the week (day 0). However, in some countries, Monday is regarded as the beginning of the week. |
Examples
const d = new Date(“2021-03-25”); d.getDay(); |
const d = new Date(); d.getDay(); |
Note You can utilize an array containing names and utilize getDay() to retrieve the weekday as a name. |
Examples
const days = [“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”]; |
const days = [“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”]; |
The getTime() function provides the count of milliseconds elapsed since January 1, 1970.
Examples
const d = new Date(“1970-01-01”); d.getTime(); |
const d = new Date(“2021-03-25”); d.getTime(); |
const d = new Date(); d.getTime(); |
Date.now() yields the count of milliseconds elapsed since January 1, 1970.
Examples
let ms = Date.now(); |
Determine the elapsed years since 1970/01/01.
const minute = 1000 * 60; const hour = minute * 60; const day = hour * 24; const year = day * 365; let years = Math.round(Date.now() / year); |
Date.now() is a static method belonging to the Date object.
It cannot be utilized on a date object in the manner of myDate.now().
Its syntax always remains as Date.now().
Method |
Same As |
Description |
getUTCDate() |
getDate() |
Returns the UTC date |
getUTCFullYear() |
getFullYear() |
Returns the UTC year |
getUTCMonth() |
getMonth() |
Returns the UTC month |
getUTCDay() |
getDay() |
Returns the UTC day |
getUTCHours() |
getHours() |
Returns the UTC hour |
getUTCMinutes() |
getMinutes() |
Returns the UTC minutes |
getUTCSeconds() |
getSeconds() |
Returns the UTC seconds |
getUTCMilliseconds() |
getMilliseconds() |
Returns the UTC milliseconds |
UTC methods operate using Coordinated Universal Time (UTC), which is equivalent to GMT (Greenwich Mean Time).
The variance between Local time and UTC time can span up to 24 hours.
The getTimezoneOffset() function provides the variance (in minutes) between local time and UTC time.
Example
let diff = d.getTimezoneOffset(); |