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

avatar

Borislav Hadzhiev

Mon Oct 25 20212 min read

banner

Photo by Chris Droukas

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 from the Date.getMonth method.

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

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

If the month or day have a value of less than 10, the getMonth and getDate methods would return a single digit. In those cases, we can add a leading zero to make the output consistent.
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 then 10 to get a consistent output.

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.

Our previous example, got the year, month and day for 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, you can use the padStart function from the last section.

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 #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee