Get Year, Month and Day from a Date Object in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
6 min

banner

# Table of Contents

  1. Get Year, Month and Day from a Date Object in JavaScript
  2. Get the Current Year, Month and Day in JavaScript
  3. Get the Day, Month, Year from a Timestamp in JavaScript

# Get Year, Month and Day from a Date Object in JavaScript

To get the year, month and day from a date object:

  1. Call the getFullYear() method to get the year.
  2. Call the getMonth() method to get an integer from 0 (January) to 11 (December).
  3. Call the getDate() method to get the day of the month.
index.js
const date = new Date(); const year = date.getFullYear(); const month = date.getMonth() + 1; const day = date.getDate(); const withSlashes = [year, month, day].join('/'); console.log(withSlashes); // ๐Ÿ‘‰๏ธ "2023/7/25" const withHyphens = [year, month, day].join('-'); console.log(withHyphens); // ๐Ÿ‘‰๏ธ "2023-7-25"

get year month day from date object

The code for this article is available on GitHub
If you need to get the current year, month and day, scroll down to the next subheading.

Notice that we added 1 to the output of the Date.getMonth() method.

index.js
const date = new Date(); const month = date.getMonth() + 1; console.log(month); // ๐Ÿ‘‰๏ธ 7

This is because months are zero-based in JavaScript, so 0 is January and 11 is December.

  • Date.getFullYear() - returns a four-digit number representing the year of the date.

  • Date.getDate() - returns an integer between 1 and 31 representing the day of the month of the date.

If the month and day have a value of less than 10, the getMonth and getDate methods will return single-digit values.

You can pad the results with a leading zero to always format the month and day as 2 digits.

index.js
// ๐Ÿ‘‡๏ธ args are YYYY, MM, DD const date = new Date(2026, 0, 5); function padTo2Digits(num) { return num.toString().padStart(2, '0'); } const year = date.getFullYear(); const month = padTo2Digits(date.getMonth() + 1); const day = padTo2Digits(date.getDate()); const withSlashes = [year, month, day].join('/'); console.log(withSlashes); // ๐Ÿ‘‰๏ธ 2026/01/05 const withHyphens = [year, month, day].join('-'); console.log(withHyphens); // ๐Ÿ‘‰๏ธ 2026-01-05

pad results with leading zero

The code for this article is available on GitHub

We used the String.padStart() method to pad the month and day if their values are less than 10.

The method takes the total length of the string and the pad string as parameters.

index.js
function padTo2Digits(num) { return num.toString().padStart(2, '0'); } console.log(padTo2Digits(4)); // ๐Ÿ‘‰๏ธ '04' console.log(padTo2Digits(8)); // ๐Ÿ‘‰๏ธ '08' console.log(padTo2Digits(10)); // ๐Ÿ‘‰๏ธ '10'

Now the month and day are guaranteed to have 2 digits.

The previous example gets the year, month and day using the visitor's local time. If you need to get the UTC equivalent, use the methods from the next code snippet.

The difference between UTC and local time is that UTC (Coordinated Universal Time) is not a time zone, but a time standard and it's the basis for time zones worldwide. No country uses UTC as a local time.

index.js
const date = new Date(); const year = date.getUTCFullYear(); const month = date.getUTCMonth() + 1; const day = date.getUTCDate(); const withSlashes = [year, month, day].join('/'); console.log(withSlashes); // ๐Ÿ‘‰๏ธ 2023/7/25 const withHyphens = [year, month, day].join('-'); console.log(withHyphens); // ๐Ÿ‘‰๏ธ 2023-7-25

We used the UTC equivalent of the getFullYear, getMonth and getDate methods.

These methods return the year, month and day according to universal time.

Just like the getMonth method, the getUTCMonth() method also returns an integer from 0 (January) to 11 (December).

If you have to get the year, month and date from a date object often, define a reusable function.

index.js
function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function getYearMonthDay(date = new Date()) { const year = date.getFullYear(); const month = padTo2Digits(date.getMonth() + 1); const day = padTo2Digits(date.getDate()); return {year, month, day}; } const {year, month, day} = getYearMonthDay(new Date(2026, 0, 5)); console.log(year); // ๐Ÿ‘‰๏ธ 2026 console.log(month); // ๐Ÿ‘‰๏ธ 01 console.log(day); // ๐Ÿ‘‰๏ธ 05
The code for this article is available on GitHub

The getYearMonthDay function takes a Date object as a parameter and returns the year, month and day of the given date.

If the function is called without a Date object, it returns the current year, month and day.

# Get the Current Year, Month and Day in JavaScript

To get the current year, month and day:

  1. Use the new Date() constructor to create a Date object.
  2. Use the getFullYear() method to get the current year.
  3. Use the getMonth() method to get the current month.
  4. Use the getDate() method to get the current day.
index.js
const date = new Date(); const currentYear = date.getFullYear(); const currentMonth = date.getMonth() + 1; const currentDay = date.getDate(); const together = [currentYear, currentMonth, currentDay].join('/'); console.log(together); // ๐Ÿ‘‰๏ธ 2023/7/25

get current year month and day

The code for this article is available on GitHub

We used the Date() to get a Date object on which we can call various methods.

The new Date() constructor returns an object that stores the current date and time.

index.js
console.log(new Date()); // ๐Ÿ‘‰๏ธ 2023-07-25T11:04:52.200Z

We called the following methods on the Date object:

  • Date.getFullYear() - returns a four-digit number representing the year of the date.

  • Date.getMonth() - returns an integer between 0 (January) and 11 (December) and represents the month of the date. Yes, unfortunately, the getMonth method is off by 1.

  • Date.getDate() - returns an integer between 1 and 31 representing the day of the month of the date.

The getFullYear() and getDate() methods work as expected, however, the getMonth() method is zero-based.

index.js
const date = new Date(); console.log(date); // ๐Ÿ‘‰๏ธ 2023-07-25T11:05:15.975Z const currentYear = date.getFullYear(); console.log(currentYear); // ๐Ÿ‘‰๏ธ 2023 const currentMonth = date.getMonth() + 1; console.log(currentMonth); // ๐Ÿ‘‰๏ธ 7 const currentDay = date.getDate(); console.log(currentDay); // ๐Ÿ‘‰๏ธ 25

This is why we added 1 to the result of calling the getMonth() method.

You can use the Intl.DateTimeFormat object to get the name of the current month in different languages, e.g. English and German.

index.js
// ๐Ÿ‘‡๏ธ Get Names of Month instead of Digits const nameOfMonthUS = new Intl.DateTimeFormat('en-US', { month: 'long', }).format(new Date()); console.log(nameOfMonthUS); // ๐Ÿ‘‰๏ธ July const nameOfMonthDE = new Intl.DateTimeFormat('de-DE', { month: 'long', }).format(new Date()); console.log(nameOfMonthDE); // ๐Ÿ‘‰๏ธ Juli console.log(new Date()); // ๐Ÿ‘‰๏ธ 2023-07-25T11:05:43.942Z

The getFullYear, getMonth and getDate methods allow us to get the year, month and day of any Date object.

If you have to get the current year, month and day often, define a reusable function.

index.js
function currentYearMonthDay() { const date = new Date(); const currentYear = date.getFullYear(); const currentMonth = date.getMonth() + 1; const currentDay = date.getDate(); return {currentYear, currentMonth, currentDay}; } const {currentYear, currentMonth, currentDay} = currentYearMonthDay(); console.log(currentYear); // ๐Ÿ‘‰๏ธ 2023 console.log(currentMonth); // ๐Ÿ‘‰๏ธ 7 console.log(currentDay); // ๐Ÿ‘‰๏ธ 25

defining reusable function

The code for this article is available on GitHub

The function returns an object containing the current year, month and day.

You can use the String.join() method if you need to format the values in any way.

index.js
const date = new Date(); function padTo2Digits(num) { return num.toString().padStart(2, '0'); } const currentYear = date.getFullYear(); const currentMonth = date.getMonth() + 1; const currentDay = date.getDate(); // ๐Ÿ‘‡๏ธ 2023/07/25 console.log( [ currentYear, padTo2Digits(currentMonth), padTo2Digits(currentDay), ].join('/'), ); // ๐Ÿ‘‡๏ธ 2023-07-25 console.log( [ currentYear, padTo2Digits(currentMonth), padTo2Digits(currentDay), ].join('-'), );
The code for this article is available on GitHub

The Array.join() method concatenates all of the elements in an array using a separator.

The only argument the Array.join() method takes is a separator - the string used to separate the elements of the array.

All you have to do is pass the specific date to the Date() constructor to initialize the date.

index.js
const date = new Date('September 24, 2025 15:24:23'); const yearOfDate = date.getFullYear(); // ๐Ÿ‘‰๏ธ 2025 const monthOfDate = date.getMonth() + 1; // 9 const dayOfMonth = date.getDate(); // 24 const together = [yearOfDate, monthOfDate, dayOfMonth].join('/'); console.log(together); // ๐Ÿ‘‰๏ธ 2025/9/24

Always keep in mind that the getMonth method is zero-based, like the index of an array or string.

To get an intuitive result, add 1 to the return value of the method.

# Get the Day, Month, Year from a Timestamp in JavaScript

To get the day, month and year values from a timestamp:

  1. Pass the timestamp to the Date() constructor to create a Date object.
  2. Use the getFullYear(), getMonth() and getDate() methods to get the year, month and day values.
index.js
const timestamp = 1643200384959; const date = new Date(timestamp); console.log(date); // ๐Ÿ‘‰๏ธ Wed Jan 26 2022 const year = date.getFullYear(); console.log(year); // ๐Ÿ‘‰๏ธ 2022 const month = date.getMonth(); console.log(month); // ๐Ÿ‘‰๏ธ 0 (January = 0, February = 1, etc) const monthName = date.toLocaleString('default', { month: 'long', }); console.log(monthName); // ๐Ÿ‘‰๏ธ "January" const day = date.getDate(); console.log(day); // ๐Ÿ‘‰๏ธ 26

get day month year from timestamp

The code for this article is available on GitHub

You can pass a timestamp (in milliseconds) to the Date() constructor to create a Date object.

We used the following 3 date-related methods:

  • Date.getFullYear() method - returns a four-digit number representing the year that corresponds to a date.

  • Date.getMonth() - returns an integer between 0 (January) and 11 (December) and represents the month for a given date. Yes, unfortunately, the getMonth method is off by 1.

  • Date.getDate() - returns an integer between 1 and 31 representing the day of the month for a specific date.

Note that the `getMonth` method returns a zero-based value, where January is 0, February is 1, March is 2, etc.

# Getting the Day, Month and Year from a timestamp according to UTC

The getFullYear, getMonth and getDate methods return the values according to the visitor's local time, if you need to get the year, month and day according to universal time, use the getUTC* methods instead.

index.js
const timestamp = 1643200384959; const date = new Date(timestamp); console.log(date); // ๐Ÿ‘‰๏ธ Wed Jan 26 2022 const year = date.getUTCFullYear(); console.log(year); // ๐Ÿ‘‰๏ธ 2022 const month = date.getUTCMonth(); console.log(month); // ๐Ÿ‘‰๏ธ 0 (January = 0, February = 1, etc) const monthName = date.toLocaleString('default', { month: 'long', }); console.log(monthName); // ๐Ÿ‘‰๏ธ January const day = date.getUTCDate(); console.log(day); // ๐Ÿ‘‰๏ธ 26
The code for this article is available on GitHub

The getUTC* methods return the values according to universal time.

There could be a difference between the output from the get* methods and the getUTC* methods if the visitor's timezone has an offset from UTC (Universal Coordinated Time).

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev