How to get year/month/day from a date object?
Categories:
Extracting Year, Month, and Day from JavaScript Date Objects

Learn how to precisely extract year, month, and day components from JavaScript Date objects using built-in methods, ensuring accurate date manipulation and display.
Working with dates is a fundamental aspect of many web applications. JavaScript's Date
object provides a robust way to handle dates and times, but often you only need specific components like the year, month, or day. This article will guide you through the standard methods to extract these individual components, along with important considerations like zero-based indexing for months and UTC versus local time.
Understanding JavaScript Date Methods
JavaScript's Date
object offers several methods to retrieve date components. It's crucial to understand the difference between methods that return values based on local time and those that return values based on Coordinated Universal Time (UTC). For most user-facing applications, local time methods are preferred, but UTC methods are essential for consistency across different time zones, especially when storing or transmitting date data.
flowchart TD A[Date Object] --> B{Local Time or UTC?} B -->|Local Time| C[getFullYear()] B -->|Local Time| D[getMonth()] B -->|Local Time| E[getDate()] B -->|UTC| F[getUTCFullYear()] B -->|UTC| G[getUTCMonth()] B -->|UTC| H[getUTCDate()] C --> I[Year] D --> I E --> I F --> I G --> I H --> I
Flowchart illustrating the decision path for extracting date components (Local vs. UTC).
Extracting Year, Month, and Day (Local Time)
To get the year, month, and day based on the user's local time zone, you'll use the following methods:
getFullYear()
: Returns the year (4 digits for 4-digit years).getMonth()
: Returns the month (0-11). Important: January is 0, December is 11. You'll typically add 1 to this value for display.getDate()
: Returns the day of the month (1-31).
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth(); // 0-indexed (0 for January)
const day = today.getDate();
console.log(`Local Date: ${year}-${month + 1}-${day}`);
// Example with a specific date
const specificDate = new Date('2023-03-15T10:00:00');
const specificYear = specificDate.getFullYear();
const specificMonth = specificDate.getMonth();
const specificDay = specificDate.getDate();
console.log(`Specific Local Date: ${specificYear}-${specificMonth + 1}-${specificDay}`);
Example of extracting year, month, and day using local time methods.
getMonth()
is zero-indexed. If you're displaying the month to a user, add 1
to the result. For example, getMonth()
returning 0
means January, so you'd display 1
.Extracting Year, Month, and Day (UTC)
For applications requiring time zone independence, such as storing dates in a database or communicating between systems, using UTC methods is best practice. These methods are identical in name to their local counterparts, but prefixed with getUTC
:
getUTCFullYear()
: Returns the UTC year.getUTCMonth()
: Returns the UTC month (0-11).getUTCDate()
: Returns the UTC day of the month (1-31).
const now = new Date();
const utcYear = now.getUTCFullYear();
const utcMonth = now.getUTCMonth(); // 0-indexed (0 for January)
const utcDay = now.getUTCDate();
console.log(`UTC Date: ${utcYear}-${utcMonth + 1}-${utcDay}`);
// Example with a specific date
const specificUtcDate = new Date('2023-03-15T10:00:00Z'); // 'Z' indicates UTC
const specificUtcYear = specificUtcDate.getUTCFullYear();
const specificUtcMonth = specificUtcDate.getUTCMonth();
const specificUtcDay = specificUtcDate.getUTCDate();
console.log(`Specific UTC Date: ${specificUtcYear}-${specificUtcMonth + 1}-${specificUtcDay}`);
Example of extracting year, month, and day using UTC methods.
Date
object, if you provide a string without time zone information (e.g., '2023-03-15'
), JavaScript will parse it as local time. If you want to explicitly create a UTC date from a string, append 'Z'
(for Zulu time, which is UTC) or provide a full ISO 8601 string with a UTC offset (e.g., '2023-03-15T00:00:00Z'
).Formatting Dates for Display
While extracting components is useful, for display purposes, you might want to use toLocaleDateString()
or Intl.DateTimeFormat
for more robust and localized formatting. These methods handle the complexities of month names, day order, and other locale-specific nuances automatically.
const myDate = new Date();
// Using toLocaleDateString()
const formattedLocal = myDate.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
console.log(`Formatted Local Date: ${formattedLocal}`); // e.g., "March 15, 2023"
// Using Intl.DateTimeFormat for more control
const formatter = new Intl.DateTimeFormat('en-GB', {
year: 'numeric',
month: '2-digit',
day: '2-digit'
});
const formattedGB = formatter.format(myDate);
console.log(`Formatted GB Date: ${formattedGB}`); // e.g., "15/03/2023"
// Getting individual parts with Intl.DateTimeFormat
const parts = new Intl.DateTimeFormat('en-US', { year: 'numeric', month: 'numeric', day: 'numeric' }).formatToParts(myDate);
const yearPart = parts.find(part => part.type === 'year').value;
const monthPart = parts.find(part => part.type === 'month').value;
const dayPart = parts.find(part => part.type === 'day').value;
console.log(`Parts from Intl: Year: ${yearPart}, Month: ${monthPart}, Day: ${dayPart}`);
Examples of formatting dates using toLocaleDateString()
and Intl.DateTimeFormat
.