Last updated: Mar 3, 2024
Reading timeยท6 min
To get the year, month and day from a date object:
getFullYear()
method to get the year.getMonth()
method to get an integer from 0
(January) to 11
(December).getDate()
method to get the day of the month.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"
Notice that we added 1
to the output of the
Date.getMonth()
method.
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.
// ๐๏ธ 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 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.
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 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.
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.
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 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.
To get the current year, month and day:
new Date()
constructor to create a Date
object.getFullYear()
method to get the current year.getMonth()
method to get the current month.getDate()
method to get the current day.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
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.
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.
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.
// ๐๏ธ 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.
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
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.
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 Array.join() method concatenates all of the elements in an array using a separator.
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.
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.
To get the day, month and year values from a timestamp:
Date()
constructor to create a Date
object.getFullYear()
, getMonth()
and getDate()
methods to get the
year, month and day values.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
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.
0
, February is 1
, March is 2
, etc.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.
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 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).