Date get methods allow you to extract specific information from a Date object (like the year, month, or day). These methods read the information but never change the original object.
| Method | Description |
|---|---|
getFullYear() |
Get the year as a four digit number (yyyy) |
getMonth() |
Get the month (0-11) |
getDate() |
Get the day as a number (1-31) |
getDay() |
Get the weekday as a number (0-6) |
getTime() |
Get the time (milliseconds since Jan 1, 1970) |
Because getMonth() and getDay() return numbers, you often need
an array to display them in a human-readable format.
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const d = new Date();
let dayName = days[d.getDay()]; // Returns name based on index
The getTime() method returns the number of milliseconds since January 1, 1970.
This value is extremely useful for performing math calculations between two
dates.
const d = new Date();
console.log(d.getTime()); // Returns a long number like 1648181234567
getFullYear(). The older getYear()
method is deprecated and may return unpredictable results in different browsers.
getFullYear() is the modern standard for year extraction
getDate() gives the day of the month (1-31)getTime() is the primary tool for date arithmetic