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

avatar

Borislav Hadzhiev

Last updated: Jul 23, 2022

banner

Check out my new book

Get Year, Month and Day from a Date Object #

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
// 👇️ in Local time const date = new Date(); const year = date.getFullYear(); // 👇️ getMonth returns integer from 0(January) to 11(December) const month = date.getMonth() + 1; const day = date.getDate(); const withSlashes = [year, month, day].join('/'); console.log(withSlashes); // 👉️ 2022/10/25 const withHyphens = [year, month, day].join('-'); console.log(withHyphens); // 👉️ 2022-10-25

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

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

The return value of the getFullYear method is the 4 digit year, and the getDate method returns an integer from 1 to 31.

If the month or the day have a value of less than 10, the getMonth and getDate methods would return a single digit. We can conditionally add a leading zero to produce consistent results.
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

We used the 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); // 👉️ 2022/10/25 const withHyphens = [year, month, day].join('-'); console.log(withHyphens); // 👉️ 2022-10-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 need to add a leading zero to the digits of the month and date if their values are less than 10, use the padStart method.

index.js
function padTo2Digits(num) { return num.toString().padStart(2, '0'); } const date = new Date(); const year = date.getUTCFullYear(); const month = padStart(date.getUTCMonth() + 1); const day = padStart(date.getUTCDate());

Further Reading #

Use the search field on my Home Page to filter through my more than 3,000 articles.