Date.getMonth() returns wrong month in JavaScript [Solved]

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
6 min

banner

# Table of Contents

  1. Date.getMonth() returns the wrong month in JavaScript
  2. Date.getDay() returns the wrong day in JavaScript
  3. getDate() returns the wrong date in JavaScript

# Date.getMonth() returns the wrong month in JavaScript

To get the correct return value from Date.getMonth(), add 1 to the result, e.g. date.getMonth() + 1.

The getMonth method returns a zero-based number between 0 and 11, where 0 is January and 11 is December.

index.js
const date = new Date('2023-04-24'); console.log(date.getMonth()); // ๐Ÿ‘‰๏ธ 3 console.log(date.getMonth() + 1); // ๐Ÿ‘‰๏ธ 4

getmonth method returns zero based number

The code for this article is available on GitHub

We used the Date() constructor to create a date for the 24th of April, 2023.

Calling the Date.getMonth() method on the date returns a value of 3, even though April is the fourth month.

This is because the getMonth method returns a zero-based number between 0 and 11. For example, January is 0, February is 1, March is 2, etc.

If you need to get a one-based value for the month, simply add 1 to the result.

index.js
const date = new Date('2023-04-24'); console.log(date.getMonth() + 1); // ๐Ÿ‘‰๏ธ 4

# The Date() constructor takes a zero-based month

When passing multiple, comma-separated parameters to the Date() constructor to create a Date object, you should pass a zero-based value for the month.

index.js
const date = new Date(2023, 0, 24, 9, 30, 16); // ๐Ÿ‘‡๏ธ 2023-01-24T07:30:16.000Z console.log(date);

date constructor takes zero based month

The code for this article is available on GitHub

The parameters we passed to the Date() constructor are the year, month (January = 0, February = 1, etc), day of the month, hour, minutes, seconds.

The Date() constructor expects the month as a zero-based value when passing multiple, comma-separated parameters.

If you need to get the name of the month, use the toLocaleString method.

index.js
const date = new Date(2023, 0, 24, 9, 30, 16); // ๐Ÿ‘‡๏ธ 2023-01-24T07:30:16.000Z console.log(date); const name = date.toLocaleString('default', { month: 'long', }); console.log(name); // ๐Ÿ‘‰๏ธ "January"

We passed the following parameters to the toLocaleString() method:

  1. the locale - e.g. en-US or de-DE, in which language the name of the month should be returned. By specifying default, it can vary based on the user's browser preferences.
  2. the options object - we set the month setting to long to get the full name of the month. Other possible values are short and narrow.
index.js
const date = new Date(2023, 0, 24, 9, 30, 16); // ๐Ÿ‘‡๏ธ January console.log(date.toLocaleString('en-US', {month: 'long'})); // ๐Ÿ‘‡๏ธ Jan console.log(date.toLocaleString('en-US', {month: 'short'})); // ๐Ÿ‘‡๏ธ J console.log(date.toLocaleString('en-US', {month: 'narrow'}));

The examples show the output from using different values for the month property on the options object.

# Date.getDay() returns the wrong day in JavaScript

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.

index.js
const date = new Date('2022-04-24'); console.log(date); // ๐Ÿ‘‰๏ธ Sun Apr 24 2022 const dayOfMonth = date.getDate(); console.log(dayOfMonth); // ๐Ÿ‘‰๏ธ 24

date getday returns wrong day

The code for this article is available on GitHub

We used the Date() constructor to create a date that stores the value of the 24th of April, 2022.

The Date.getDate() method returns the day of the month stored in the specific Date object.

The method is often confused with Date.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.

index.js
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().

The method 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.

index.js
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 code for this article is available on GitHub

The first thing we did was create a padTo2Digits function, which will take care of adding a leading zero if the month or day only contains a single digit (are less than 10).

index.js
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 String.padStart() method.

The first parameter we passed to the 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.

The 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.

index.js
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.

# getDate() returns the wrong date in JavaScript

The getDate() method might return a wrong date when passing a string to the Date() constructor, e.g. new Date(dateString). How date strings are parsed is implementation-dependent and might vary between browsers.

For consistent results when creating a Date object, pass multiple, comma-separated values for the year, month, day, hour, minutes and seconds to the Date() constructor.

index.js
// ๐Ÿ‘‡๏ธ formatted as YYYY-MM-DD hh:mm:ss const dateStr = '2022-09-24 07:30:24'; const [dateComponents, timeComponents] = dateStr.split(' '); console.log(dateComponents); // ๐Ÿ‘‰๏ธ 2022-09-22 console.log(timeComponents); // ๐Ÿ‘‰๏ธ 07:30:24 const [year, month, day] = dateComponents.split('-'); const [hours, minutes, seconds] = timeComponents.split(':'); // โœ… Create Date using multiple, comma-separated parameters const date = new Date(+year, +month - 1, +day, +hours, +minutes, +seconds); console.log(date); // ๐Ÿ‘‰๏ธ Sat Sep 24 2022 07:30:24 console.log(date.getDate()); // ๐Ÿ‘‰๏ธ 24
The code for this article is available on GitHub

How the Date() constructor interprets the passed in Date() strings can vary between implementations.

This could be the cause of the getDate method returning a wrong date.

A common issue when creating dates with date strings is that if you live in an area that's behind GMT, you might get a date of the previous day.

If you create a date with a date string (without specifying the time), you get a date set in UTC.

For example, if you create a new Date('2022-09-24'), you end up creating a date for the 24th of September 2022, 12 AM UTC.

But if you live in an area behind GMT, your Date object might point to the 23rd of September, instead of the 24th.

If you want to create a date that is in local time with a date string, you need to include the time, e.g. new Date(2022-09-24T00:00:00).

Creating a Date object using a date string can be quite confusing, so it's best to use multiple, comma-separated parameters instead.

A safe approach is to pass the year, month (January = 0, February = 1, etc), day, hour, minutes and seconds values as comma-separated parameters to the constructor.

If you don't have values for the time, you can just pass the year, month (zero-based) and day of the month as parameters when creating the Date object.

In the example, we had a date and time string, formatted as YYYY-MM-DD hh:mm:ss, but this could be any other format.

The first thing we did was String.split() the date and time string on the space, so we can get the date and time components as separate strings.

We then had to split the date string on each hyphen to get the value for the month, day and year. Note that your separator might be different, e.g. a forward slash, but the approach is the same.

We also split the time string on each colon and assigned the hours, minutes and seconds to variables.

Notice that we subtracted 1 from the month when passing it to the Date() constructor.

This is because, the Date constructor expects a zero-based value, where January = 0, February = 1, March = 2, etc.

We passed all of the parameters to the Date() constructor to create a Date object and got the correct value from the getDate() method.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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