Get the Number of Days in a Month or a Year in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
5 min

banner

# Table of Contents

  1. Get the Number of Days in a Month in JavaScript
  2. Get the Number of Days in the Current Month
  3. Get the Day of the Year (1 - 366) using JavaScript

# Get the Number of Days in a Month in JavaScript

To get the number of days in a month:

  1. Call the new Date() constructor, passing it 0 for the days.
  2. The method will return the date corresponding to the last day of the month.
  3. Call the getDate() method on the result to get the number of days in the month.
index.js
function getDaysInMonth(year, month) { return new Date(year, month, 0).getDate(); } const date = new Date(); const currentYear = date.getFullYear(); const currentMonth = date.getMonth() + 1; // ๐Ÿ‘ˆ๏ธ months are 0-based // ๐Ÿ‘‡๏ธ Current Month const daysInCurrentMonth = getDaysInMonth( currentYear, currentMonth, ); console.log(daysInCurrentMonth); // ๐Ÿ‘‰๏ธ 31 // ๐Ÿ‘‡๏ธ Other Months const daysInJanuary = getDaysInMonth(2025, 1); console.log(daysInJanuary); // ๐Ÿ‘‰๏ธ 31 const daysInSeptember = getDaysInMonth(2025, 9); console.log(daysInSeptember); // ๐Ÿ‘‰๏ธ 30

get number of days in month

The code for this article is available on GitHub

We passed the following 3 arguments to the new Date() constructor:

  1. The year for which we want to create a Date object
  2. The month index for which we want to create a Date object. Month indexes are zero-based where 0 is January and 11 is December.
  3. The day for which to create a Date object. When 0 is used for the days, we get back the last day of the previous month. We use this approach to balance out the zero-based month index.
If you're passing the result of calling getMonth() to the function, make sure to add 1 to the month index.
index.js
const date = new Date(); // ๐Ÿ‘‡๏ธ add 1 to get correct value const currentMonth = date.getMonth() + 1;

Specifying 0 for the days parameter, allows us to use intuitive month indexes when using the new Date() constructor.

For example, passing a month index of 2, gives us the last day in February, and not March.

index.js
console.log(new Date(2025, 1, 0)); // ๐Ÿ‘‰๏ธ Fri January 31 2025 console.log(new Date(2025, 2, 0)); // ๐Ÿ‘‰๏ธ Fri Feb 28 2025
This returns an object that represents the last day of the month.

The last step is to call the Date.getDate() method.

index.js
console.log(new Date(2025, 1, 0).getDate()); // ๐Ÿ‘‰๏ธ 31 console.log(new Date(2025, 2, 0).getDate()); // ๐Ÿ‘‰๏ธ 28

The method returns an integer from 1 to 31, which represents the day of the month for a given date according to local time.

Getting the integer representation of the last day of the month is the equivalent of getting the number of days in the month.

# Get the Number of Days in the Current Month in JavaScript

To get the number of days in the current month:

  1. Use the new Date() constructor to get a date object that corresponds to the last day of the current month.
  2. Call the getDate() method to get an integer representing the last day of the month.
index.js
function getDaysInCurrentMonth() { const date = new Date(); return new Date( date.getFullYear(), date.getMonth() + 1, 0, ).getDate(); } const result = getDaysInCurrentMonth(); console.log(result); // ๐Ÿ‘‰๏ธ 31

get number of days in current month

The code for this article is available on GitHub

We passed the following 3 arguments to the new Date() constructor:

  1. The current year.
  2. The current month index. Month indexes are zero-based so 0 is January and 11 is December. This is why we added 1 to the result.
  3. The day for which to create a date object. When 0 is used as the day, we get back the last day of the previous month. We use this approach to balance out the zero-based month index.

It's quite confusing but the getMonth() method returns a zero-based index.

index.js
const date = new Date('January 04, 2025 05:24:07'); console.log(date.getMonth()); // ๐Ÿ‘‰๏ธ 0

To balance this out, we passed 0 to the days parameter of the new Date() constructor to get the last day of the prior month.

For example, passing a month index of 2, gives us the last day of February and not March.

index.js
console.log(new Date(2025, 1, 0)); // ๐Ÿ‘‰๏ธ Fri January 31 2025 console.log(new Date(2025, 2, 0)); // ๐Ÿ‘‰๏ธ Fri Feb 28 2025
In the function, the new Date() constructor returns an object representing the last day of the current month.

The last step is to call the Date.getDate() method.

index.js
function getDaysInCurrentMonth() { const date = new Date(); return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate(); } const result = getDaysInCurrentMonth(); console.log(result); // ๐Ÿ‘‰๏ธ 31

The method returns an integer (from 1 to 31) that represents the day of the month for a given date.

Getting the integer representation of the last day of the current month is the equivalent of getting the number of days in the month.

# Get the Day of the Year (1 - 366) using JavaScript

To get the day of the year:

  1. Get a timestamp that represents the date.
  2. Get a timestamp that represents the last day of the previous year.
  3. Subtract the second timestamp from the first and convert the result to days.
index.js
function getDayOfYear(date = new Date()) { const timestamp1 = Date.UTC( date.getFullYear(), date.getMonth(), date.getDate(), ); const timestamp2 = Date.UTC(date.getFullYear(), 0, 0); const differenceInMilliseconds = timestamp1 - timestamp2; const differenceInDays = differenceInMilliseconds / 1000 / 60 / 60 / 24; return differenceInDays; } console.log(getDayOfYear(new Date('2022-02-01'))); // ๐Ÿ‘‰๏ธ 32 console.log(getDayOfYear(new Date('2022-01-29'))); // ๐Ÿ‘‰๏ธ 29 console.log(getDayOfYear(new Date('2022-03-01'))); // ๐Ÿ‘‰๏ธ 60 console.log(getDayOfYear(new Date('2022-12-31'))); // ๐Ÿ‘‰๏ธ 365

get day of year using javascript

The code for this article is available on GitHub

We created a reusable function that returns the number of the day of the year.

The function takes a Date object as a parameter or uses the current date if none is provided

The Date.UTC() method takes similar parameters to the Date() constructor, but treats them as UTC and returns the number of milliseconds since the 1st of January 1970, 00:00:00 UTC.

We used the Date.UTC method to avoid any Daylight Saving Time (DST) issues.

In the first call to the method, we got a timestamp of the passed-in date.

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.

In our second call to the Date.UTC() method, we got a timestamp for the last day of the previous year.

index.js
function getDayOfYear(date = new Date()) { const timestamp1 = Date.UTC( date.getFullYear(), date.getMonth(), date.getDate(), ); const timestamp2 = Date.UTC(date.getFullYear(), 0, 0); const differenceInMilliseconds = timestamp1 - timestamp2; const differenceInDays = differenceInMilliseconds / 1000 / 60 / 60 / 24; return differenceInDays; }
The code for this article is available on GitHub

We passed 0 for the month of January and 0 for the day.

Note that the value for the day of the month is not zero-based, it is one-based.

The Date object in JavaScript automatically takes care of rolling back or rolling over the values for the month and year, so we got a timestamp for the last day of the previous year.

Subtracting the second timestamp from the first gives us the difference between the passed-in date and the last day of the previous year in milliseconds.

The last step is to convert the difference from milliseconds to days to get the day of the year.

# 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