JavaScript provides various methods to retrieve date and time components from a Date
object. These methods allow you to extract specific information such as the year, month, day, hour, etc.
1. getFullYear()
Returns the year of the specified date according to local time.
let date = new Date(); console.log(date.getFullYear()); // Output: 2025 (or the current year)
2. getMonth()
Returns the month (0-11) of the specified date according to local time. Note that January is 0
and December is 11
.
console.log(date.getMonth()); // Output: 0 (for January)
3. getDate()
Returns the day of the month (1-31) for the specified date according to local time.
console.log(date.getDate()); // Output: 19 (for the 19th of the month)
4. getDay()
Returns the day of the week (0-6) for the specified date according to local time. Sunday is 0
, Monday is 1
, and so on.
console.log(date.getDay()); // Output: 0 (for Sunday)
5. getHours()
Returns the hour (0-23) of the specified date according to local time.
console.log(date.getHours()); // Output: 10 (for 10 AM)
6. getMinutes()
Returns the minutes (0-59) of the specified date according to local time.
console.log(date.getMinutes()); // Output: 20
7. getSeconds()
Returns the seconds (0-59) of the specified date according to local time.
console.log(date.getSeconds()); // Output: 30
8. getMilliseconds()
Returns the milliseconds (0-999) of the specified date according to local time.
console.log(date.getMilliseconds()); // Output: 0-999
9. getTime()
Returns the numeric value corresponding to the time for the specified date since January 1, 1970, 00:00:00 UTC (the Unix Epoch).
console.log(date.getTime()); // Output: 1734561230000 (milliseconds since Unix Epoch)
10. getTimezoneOffset()
Returns the difference in minutes between UTC and the local time.
console.log(date.getTimezoneOffset()); // Output: -60 (for UTC+1)
11. getUTCFullYear()
Returns the year of the specified date according to universal time.
console.log(date.getUTCFullYear()); // Output: 2025
12. getUTCMonth()
Returns the month (0-11) of the specified date according to universal time.
console.log(date.getUTCMonth()); // Output: 0
13. getUTCDate()
Returns the day of the month (1-31) of the specified date according to universal time.
console.log(date.getUTCDate()); // Output: 19
14. getUTCDay()
Returns the day of the week (0-6) of the specified date according to universal time.
console.log(date.getUTCDay()); // Output: 0
15. getUTCHours()
Returns the hour (0-23) of the specified date according to universal time.
console.log(date.getUTCHours()); // Output: 10
16. getUTCMinutes()
Returns the minutes (0-59) of the specified date according to universal time.
console.log(date.getUTCMinutes()); // Output: 20
17. getUTCSeconds()
Returns the seconds (0-59) of the specified date according to universal time.
console.log(date.getUTCSeconds()); // Output: 30
18. getUTCMilliseconds()
Returns the milliseconds (0-999) of the specified date according to universal time.
console.log(date.getUTCMilliseconds()); // Output: 500
Summary of JavaScript Date Get Methods
getFullYear()
,getMonth()
,getDate()
: Retrieve year, month, and day of the month.getDay()
: Get the day of the week.getHours()
,getMinutes()
,getSeconds()
: Get time components.getTime()
: Get the timestamp (milliseconds since the Unix Epoch).getUTC*()
methods: Similar to the above, but in UTC.
These methods are essential for working with dates and times in JavaScript, allowing developers to access and manipulate various components easily.