Skip to main content

How to Get the Number of Days in a Month and Day of the Year in JavaScript

When working with dates, you often need to answer specific questions about them, such as "How many days are in this month?" or "What day of the year is this?" JavaScript's Date object provides all the tools you need to solve these problems, but it requires understanding a few clever tricks.

This guide will teach you the modern, standard methods for calculating both the number of days in a given month and the day of the year for any given date.

Goal 1: Get the Number of Days in a Month

To find out how many days are in a specific month (e.g., 31 for January, 28 or 29 for February), we can use a clever feature of the Date constructor.

The Core Trick: The "Zeroth" Day

The new Date(year, monthIndex, day) constructor is smart enough to handle out-of-range values. If you ask for the "0th" day of a month, it will roll back to the last day of the previous month.

We can use this to our advantage. To get the last day of a given month, we simply ask for the 0th day of the next month.

This function encapsulates the logic for a clean and reusable solution.

/**
* Gets the number of days in a specific month.
* @param {number} year The full year.
* @param {number} month The zero-based month index (0-11).
* @returns {number} The number of days in the month.
*/
function getDaysInMonth(year, month) {
// The '0' for the day gets the last day of the PREVIOUS month.
// So, we ask for the next month (month + 1) to get the end of the current one.
return new Date(year, month + 1, 0).getDate();
}

Solution

Now you can use this function to get the number of days for any month.

Example 1: Get the number of days in February 2024 (a leap year)

// Remember: The month is 0-indexed, so 1 = February
const daysInFeb = getDaysInMonth(2024, 1);
console.log(daysInFeb);

Output:

29

Example 2: Get the number of days in the CURRENT month

const today = new Date();
const daysInCurrentMonth = getDaysInMonth(today.getFullYear(), today.getMonth());
console.log(`There are ${daysInCurrentMonth} days in this month.`);

Output: (if current month is October)

There are 31 days in this month.

Goal 2: Get the Day of the Year (1-366)

To find the "ordinal day" (the Nth day of the year), we need to calculate the difference between the start of the year and the target date.

The Core Logic:

  1. Get the timestamp for the start of the target date's year (January 1st, at midnight).
  2. Get the timestamp for the target date.
  3. Calculate the difference between these two timestamps in milliseconds.
  4. Convert the millisecond difference into days and round up.
note

Best Practice: Use Date.UTC() to perform these calculations. This avoids potential bugs caused by Daylight Saving Time, as it keeps all calculations in a consistent timezone.

/**
* Calculates the day of the year for a given date.
* @param {Date} date The date to check.
* @returns {number} The day number (1-366).
*/
function getDayOfYear(date) {
// Get the timestamp for the start of the year (in UTC)
const startOfYear = Date.UTC(date.getUTCFullYear(), 0, 1);

// Get the timestamp for the given date (in UTC)
const today = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate());

const oneDayInMs = 1000 * 60 * 60 * 24;

// Calculate the difference in days and add 1 (because Jan 1st is day 1)
return Math.floor((today - startOfYear) / oneDayInMs) + 1;
}

Solution

Example 1: Check February 1st, 2024

const myDate = new Date('2024-02-01');
console.log(getDayOfYear(myDate));

Output:

32
note

Output is 32 because there are 31 days in January + 1 of the first of February.

Example 2: Check the last day of a non-leap year

const lastDay = new Date('2025-12-31');
console.log(getDayOfYear(lastDay));

Output:

365

Conclusion

JavaScript's Date object provides all the necessary tools for these common calculations, as long as you know the right tricks.

  • To get the number of days in a month, use the "zeroth day" trick with the Date constructor: new Date(year, month + 1, 0).getDate().
  • To get the day of the year, calculate the difference in timestamps between your date and the first day of that year, preferably using Date.UTC() to avoid timezone issues.

By encapsulating these patterns in reusable functions, you can write clean, readable, and reliable date-handling code.