Borislav Hadzhiev
Sun Jan 23 2022·2 min read
Photo by Farsai Chaikulngamdee
Use the getDate()
method, instead of getDay()
, to get the correct value
for the day of the month, e.g. date.getDate()
. The getDate
method returns a
number between 1
and 31
that represents the day of the month for the given
date.
const date = new Date('2022-04-24'); console.log(date); // 👉️ Sun Apr 24 2022 const dayOfMonth = date.getDate(); console.log(dayOfMonth); // 👉️ 24
We used the Date() constructor to create a date that stores the value of 24th of April, 2022.
The
getDate()
method returns the day of the month stored in the specific Date
object.
The method is often confused with getDay().
The getDay
method returns a number between 0
and 6
that corresponds to the
day of the week for the given date.
For example, 0
is Sunday, 1
is Monday, 2
is Tuesday, etc.
const date = new Date('2022-04-24'); console.log(date); // 👉️ Sun Apr 24 2022 // ❌ getDay = zero-based day of week const dayOfWeek = date.getDay(); console.log(dayOfWeek); // 👉️ 0
Another confusing date-related method is getMonth()
, which returns a
zero-based number for the month of a given day, where 0
is January, 1
is
February, 2
is March, etc.
Here is an example that formats a Date
as YYYY-MM-DD
.
function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function formatDate(date) { return [ date.getFullYear(), padTo2Digits(date.getMonth() + 1), padTo2Digits(date.getDate()), ].join('-'); } const date = new Date('2022-04-24'); // 👇️ 2022-04-24 (yyyy-mm-dd) console.log(formatDate(date)); // 👇️️ 2025-05-09 (yyyy-mm-dd) console.log(formatDate(new Date(2025, 4, 9)));
The first thing we did is create a padTo2Digits
function, which will take care
of adding a leading zero if the month or day only contain a single digit (are
less than 10
).
function padTo2Digits(num) { return num.toString().padStart(2, '0'); } console.log(padTo2Digits(3)); // 👉️ '03' console.log(padTo2Digits(6)); // 👉️ '06' console.log(padTo2Digits(10)); // 👉️ '10'
We want to make sure that the result is always consistent and has 2 digits for the months and days, so we used the padStart method.
padTo2Digits
function is the total length of the string, so it will never pad the day or month if they already have 2 digits.Next, we created a function that takes a date and formats it to yyyy-mm-dd
.
The function makes use of 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.
getMonth
method returns a zero-based month index from 0 to 11, meaning January is 0
and December is 11
.Because the getMonth
method is zero-based we added 1 to its return value.
The last step is to place the calls to the methods in an array, so we can join them by a hyphen separator. Here are some examples.
console.log(['2022', '04', '24'].join('-')); // 👉️ '2022-04-24' console.log(['2024', '09', '15'].join('-')); // 👉️ '2024-09-15'
We used a hyphen separator in the example, however you could use a forward slash
/
, reorder the values, or make any other changes.