JavaScript Date instances provide get methods to extract individual date/time components (year, month, day of week, hours, minutes) in either local client time or UTC.
flowchart TD
GetMethods["Date Get Methods"] --> Local["Local Time: getFullYear(), getMonth(), getDate(), getDay()"]
GetMethods --> UTC["UTC Time: getUTCFullYear(), getUTCMonth(), getUTCDate()"]
GetMethods --> Time["Timestamp: getTime(), getTimezoneOffset()"]
| Method | Return Range / Unit | Explanation |
|---|---|---|
getFullYear() |
4-digit Year (e.g. 2026) |
Returns 4-digit calendar year. |
getMonth() |
0 - 11 |
Returns month (0 = January, 11 = December). |
getDate() |
1 - 31 |
Returns day of the month. |
getDay() |
0 - 6 |
Returns day of the week (0 = Sunday, 6 = Saturday). |
getHours() |
0 - 23 |
Returns hour of day (24-hour clock). |
getTimezoneOffset() |
Minutes | Returns offset between local time and UTC in minutes. |
// Demonstrating Date Get Methods for component extraction
const currentDate = new Date();
console.log("--- Local Date Components ---");
console.log(`Full Year: ${currentDate.getFullYear()}`);
console.log(`Month (0-indexed): ${currentDate.getMonth()}`); // Add +1 for human month
console.log(`Date of Month: ${currentDate.getDate()}`);
console.log(`Day of Week (0=Sun): ${currentDate.getDay()}`);
console.log(`Hours (24h): ${currentDate.getHours()}`);
console.log(`Minutes: ${currentDate.getMinutes()}`);
console.log("
--- UTC Date Components ---");
console.log(`UTC Full Year: ${currentDate.getUTCFullYear()}`);
console.log(`UTC Month: ${currentDate.getUTCMonth()}`);
console.log(`UTC Date: ${currentDate.getUTCDate()}`);
console.log("
--- Timezone Offset ---");
const offsetMinutes = currentDate.getTimezoneOffset();
console.log(`Timezone Offset from UTC: ${offsetMinutes} minutes (${offsetMinutes / 60} hours)`);
getYear(): Legacy getYear() returns 2-digit years minus 1900. Always use getFullYear().getDate() vs getDay(): getDate() returns the numeric day of the month (1-31). getDay() returns the day of the week (0-6).getMonth() for Display: When constructing human-readable date strings manually, remember to add + 1 to getMonth().Write a function getDayName(date) that converts date.getDay() into its corresponding string name (e.g. "Sunday", "Monday").
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.