Course contentsShow
← JavaScript
Lesson 1622 of 2,72335. Standard Library and Built-insFree lesson

Getting Date Components: getFullYear, getMonth, getDate

Extract year, month, and day values from Date objects and understand zero-based month indexing.

Getting Date Components: getFullYear, getMonth, getDate

You'll learn how to extract specific year, month, and day values from Date objects using built-in getter methods.


Once you have a Date object, you often need to pull out individual pieces like "What year is this?" or "What day of the month?" JavaScript provides straightforward methods for this:

Extracting the Year

getFullYear() returns the four-digit year:

const date = new Date('2024-05-15');
console.log(date.getFullYear()); // 2024

Extracting the Month (Zero-Based!)

getMonth() returns the month as a zero-based index (0 = January, 11 = December):

const date = new Date('2024-05-15');
console.log(date.getMonth()); // 4 (May is the 5th month, but zero-indexed)

This is the trickiest part: JavaScript counts months starting from 0, not 1. If you need the human-readable month number, add 1:

console.log(date.getMonth() + 1); // 5

Extracting the Day of the Month

getDate() returns the day of the month (1-31):

const date = new Date('2024-05-15');
console.log(date.getDate()); // 15

Practical Example

const now = new Date('2024-12-25');
const year = now.getFullYear();  // 2024
const month = now.getMonth();     // 11 (December)
const day = now.getDate();        // 25

console.log(`${year}-${month + 1}-${day}`); // "2024-12-25"

Think of these methods as pulling labeled drawers from a filing cabinet—each drawer contains one specific piece of date information, already separated for you.

Key Takeaway: Use getFullYear(), getMonth() (zero-based!), and getDate() to extract year, month, and day components from Date objects—just remember that months are indexed from 0 to 11, not 1 to 12.