How to get a number of days in a specified month using JavaScript?

To get the number of days in a specified month in JavaScript, you can use the Date constructor with a clever trick: create a date for the first day of the next month, then subtract one day to get the last day of the target month.

The Date Constructor Trick

The key insight is that new Date(year, month, 0) returns the last day of the previous month. Since JavaScript months are zero-indexed (0 = January, 1 = February, etc.), passing the actual month number gives us the last day of that month.

Basic Implementation

<!DOCTYPE html>
<html>
   <body>
      <script>
         function getDaysInMonth(month, year) {
            return new Date(year, month, 0).getDate();
         }

         document.write("Days in July: " + getDaysInMonth(7, 2012) + "<br>");
         document.write("Days in September: " + getDaysInMonth(9, 2012) + "<br>");
         document.write("Days in February 2020 (leap year): " + getDaysInMonth(2, 2020));
      </script>
   </body>
</html>
Days in July: 31
Days in September: 30
Days in February 2020 (leap year): 29

How It Works

When you call new Date(2012, 7, 0), JavaScript creates a date object for day 0 of month 7 (August), which automatically rolls back to the last day of month 6 (July). The getDate() method then returns that day number.

Alternative Approaches

Using Month Names

<!DOCTYPE html>
<html>
   <body>
      <script>
         function getDaysInMonthByName(monthName, year) {
            const monthNames = ["January", "February", "March", "April", "May", "June",
                              "July", "August", "September", "October", "November", "December"];
            const monthIndex = monthNames.indexOf(monthName) + 1;
            return new Date(year, monthIndex, 0).getDate();
         }

         document.write("Days in February 2024: " + getDaysInMonthByName("February", 2024) + "<br>");
         document.write("Days in April 2023: " + getDaysInMonthByName("April", 2023));
      </script>
   </body>
</html>
Days in February 2024: 29
Days in April 2023: 30

Handling Current Month

<!DOCTYPE html>
<html>
   <body>
      <script>
         function getDaysInCurrentMonth() {
            const today = new Date();
            const year = today.getFullYear();
            const month = today.getMonth() + 1;
            return new Date(year, month, 0).getDate();
         }

         document.write("Days in current month: " + getDaysInCurrentMonth());
      </script>
   </body>
</html>

Key Points

  • JavaScript months are zero-indexed (January = 0, February = 1, etc.)
  • Setting day to 0 automatically goes to the last day of the previous month
  • This method automatically handles leap years for February
  • Works for any valid year and month combination

Conclusion

The new Date(year, month, 0).getDate() approach is the most reliable way to get days in a month. It automatically handles leap years and different month lengths without complex logic.

Updated on: 2026-03-15T23:18:59+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements