How do you know if a month has 30 or 31 days javascript?

Brett DeWoody

56.9k28 gold badges135 silver badges183 bronze badges

asked Jul 26, 2009 at 11:27

1

// Month in JavaScript is 0-indexed (January is 0, February is 1, etc), 
// but by using 0 as the day it will give us the last day of the prior
// month. So passing in 1 as the month number will return the last day
// of January, not February
function daysInMonth (month, year) {
    return new Date(year, month, 0).getDate();
}

// July
daysInMonth(7,2009); // 31
// February
daysInMonth(2,2009); // 28
daysInMonth(2,2008); // 29

How do you know if a month has 30 or 31 days javascript?

answered Jul 26, 2009 at 11:36

16

Date.prototype.monthDays= function(){
    var d= new Date(this.getFullYear(), this.getMonth()+1, 0);
    return d.getDate();
}

answered Jul 26, 2009 at 22:57

kennebeckennebec

100k31 gold badges104 silver badges126 bronze badges

3

The following takes any valid datetime value and returns the number of days in the associated month... it eliminates the ambiguity of both other answers...

 // pass in any date as parameter anyDateInMonth
function daysInMonth(anyDateInMonth) {
    return new Date(anyDateInMonth.getFullYear(), 
                    anyDateInMonth.getMonth()+1, 
                    0).getDate();}

Biruk Abebe

2,2041 gold badge11 silver badges24 bronze badges

answered Jul 26, 2009 at 17:28

Charles BretanaCharles Bretana

140k22 gold badges145 silver badges213 bronze badges

7

Another possible option would be to use Datejs

Then you can do

Date.getDaysInMonth(2009, 9)     

Although adding a library just for this function is overkill, it's always nice to know all the options you have available to you :)

yckart

30.9k9 gold badges117 silver badges127 bronze badges

answered Sep 23, 2009 at 8:30

RYFNRYFN

2,8671 gold badge29 silver badges37 bronze badges

2

Get the Number of Days in a Month #

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.

Copied!

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); // 👇️ Other Months const daysInJanuary = getDaysInMonth(2025, 1); console.log(daysInJanuary); // 👉️ 31 const daysInSeptember = getDaysInMonth(2025, 9); console.log(daysInSeptember); // 👉️ 30

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

Copied!

const date = new Date(); // 👇️ add 1 to get correct value const currentMonth = date.getMonth() + 1;

Passing 0 for the days parameter, allows us to use intuitive month indexes when using the Date constructor.

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

Copied!

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. The method returns an integer from 1 to 31, which represents the day of the month for a given date.

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

Further Reading #

  • Get the Number of Days in the Current Month in JavaScript
  • How to get Current Year, Month and Day in JavaScript

How do you know if a month has 31 days JavaScript?

getDate method. The method returns an integer from 1 to 31 , which represents the day of the month for a given date. Getting the integer representation of the last day of the month is the equivalent of getting the number of days in the month.

How do I know if a date is within 30 days?

To check if a date is within 30 days:.
Subtract the timestamp of the current date from the timestamp of the date..
Pass the result to the Math. abs() function..
Convert the result to days..
Check if the days between the dates is less than 30 ..

How do you get no of days in a month in JS?

Get Number of Days in Month Using JavaScript.
const getDays = (year, month) => { return new Date(year, month, 0). getDate(); };.
const daysInSeptember = getDays(2021, 7); // Returns 31..
const daysInSeptember = getDays(new Date(). getFullYear(), 7); // Returns 31..