Skip to main content

How to Get the Year, Month, and Day from a Date in JavaScript

Extracting individual components—like the year, month, and day—from a Date object is a fundamental task in JavaScript. This is essential for formatting dates, performing calculations, or populating UI elements. JavaScript's Date object provides a standard set of methods for this purpose.

This guide will teach you the core methods for getting these components. You will learn the critical "gotcha" of the getMonth() method and understand the important distinction between getting values in local time versus UTC.

The Core Methods: getFullYear(), getMonth(), and getDate()

JavaScript's Date prototype provides a straightforward set of "getter" methods to access its components.

  • getFullYear(): Returns the four-digit year (e.g., 2023).
  • getMonth(): Returns the month as a zero-based index (0-11).
  • getDate(): Returns the day of the month (1-31).

The Most Important Rule: getMonth() is Zero-Based

This is the single biggest source of bugs for developers working with JavaScript dates. The getMonth() method does not return a number from 1 to 12.

  • 0 = January
  • 1 = February
  • ...
  • 11 = December

If you need to display the month number to a user or use it in a 1-12 format, you must add 1 to the result.

const myDate = new Date('2025-10-16');      // An October date

const monthIndex = myDate.getMonth(); // This is 9
const monthNumber = myDate.getMonth() + 1; // This is 10

console.log(monthIndex); // Output: 9
console.log(monthNumber); // Output: 10

Local Time vs. UTC

By default, the methods above return values based on the user's local timezone. For server-side applications or when you need a consistent, timezone-agnostic value, you should use the UTC equivalents:

  • getUTCFullYear()
  • getUTCMonth()
  • getUTCDate()

These methods return the date components corresponding to Coordinated Universal Time (UTC), regardless of where the code is being run.

// A date object representing a specific moment in time
const myDate = new Date('2025-01-01T02:00:00.000Z'); // 2 AM in UTC

// In a US timezone (e.g., UTC-5), this is still the previous day locally
console.log(myDate.getDate()); // Output: 31 (for December 31st)
console.log(myDate.getUTCDate()); // Output: 1 (for January 1st)

Practical Examples

The same core methods are used whether you are working with a specific date, the current date, or a timestamp.

How to Get Components from a Specific Date Object

This is the most direct use case.

const myDate = new Date(2025, 4, 9);    // May 9, 2025

const year = myDate.getFullYear(); // 2025
const month = myDate.getMonth() + 1; // 5
const day = myDate.getDate(); // 9

console.log(`${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`);

Output:

2025-05-09

How to Get Components from the Current Date

To get the components for today, simply create a new Date() object with no arguments.

const today = new Date();

const currentYear = today.getFullYear();
const currentMonth = today.getMonth() + 1;
const currentDay = today.getDate();

console.log(`Today is: ${currentMonth}/${currentDay}/${currentYear}`);

Output:

Today is: 10/16/2025

How to Get Components from a Timestamp

If you have a Unix timestamp (in milliseconds), first convert it to a Date object, then use the same methods.

const timestamp = 1760645527;               // Unix timestamp in seconds
const myDate = new Date(timestamp * 1000); // Convert to milliseconds

const year = myDate.getUTCFullYear();
const month = myDate.getUTCMonth() + 1; // Months are zero-based
const day = myDate.getUTCDate();

console.log(`Year: ${year}, Month: ${month}, Day: ${day}`);

Output:

Year: 2023, Month: 10, Day: 27

Conclusion

Getting the year, month, and day from a Date object is a simple task if you remember a few key rules.

  • Use getFullYear() for the year, getMonth() for the month, and getDate() for the day of the month.
  • Always add 1 to the result of getMonth() for a standard 1-12 month representation.
  • Use the UTC methods (getUTCFullYear(), etc.) when you need a consistent, timezone-agnostic value.