Excel Tutorial: Calculate Nights Between Dates in Each Month

Published on

When working with date ranges in Excel, you might need to calculate how many nights fall within each month. This is especially useful for scenarios like tracking overnight stays with check-in and check-out dates.

While Excel doesn’t have a built-in function to split dates across months, you can achieve this using a combination of formulas.

In this tutorial, we’ll use a dynamic array formula to Calculate Nights in Each Month in Excel. Once the data is ready, we’ll summarize it using either a SUMIF formula or a Pivot Table for a month-wise summary.

Step 1: Use a Dynamic Array Formula to Calculate Nights in Each Month

See the sample data below:

Sample Data for Check-in and Check-out Dates in Excel

The check-in dates are in A2:A9, and the check-out dates are in B2:B9, with headers in row 1. Use the following formula in cell D1 to calculate the nights in each month:

=LET(cal,IFNA(DROP(REDUCE(0,A2:A9,LAMBDA(acc,val,LET(in,val,out,OFFSET(val,0,1),VSTACK(acc,HSTACK(EOMONTH(in,-1)+1,EOMONTH(out,-1)+1,IF(EOMONTH(in,0)<>EOMONTH(out,0),HSTACK(DAYS(EOMONTH(out,-1),in)+1,DAYS(out,EOMONTH(out,-1)+1)),DAYS(out,in))))))), 1), 0), HSTACK(VSTACK("Month", CHOOSECOLS(cal,1),CHOOSECOLS(cal,2)),VSTACK("Nights", CHOOSECOLS(cal,3),CHOOSECOLS(cal,4))))

This formula will return a two-column array with the headers Month and Nights:

  • The Month column contains the start dates of the months, which are date values. You should select these cells and apply date formatting by clicking Short Date within the Number group on the Home tab.
  • The Nights column contains the number of nights for each month.
Dynamic Array Formula for Calculating Nights in Each Month in Excel

Note: There may be multiple occurrences of the same month depending on the bookings.

Formula Explanation

The formula calculates the number of nights for both the check-in and check-out months:

For example, if the check-in date is 28-12-2024 and the check-out date is 02-01-2025, it will return:

  • 4 nights for December 2024 (from the 28th to the 31st).
  • 1 night for January 2025 (on the 1st).
IF(EOMONTH(A2, 0)<>EOMONTH(B2, 0), HSTACK(DAYS(EOMONTH(B2, -1), A2)+1, DAYS(B2, EOMONTH(B2, -1)+1)), DAYS(B2, A2))

The formula then appends the beginning of each month’s date:

=HSTACK(EOMONTH(A2, -1)+1, EOMONTH(B2, -1)+1, ...)

This results in:

01-12-202401-01-202541

Next, we vertically stack these dates and nights into two columns:

=HSTACK(VSTACK("Month", CHOOSECOLS(...,1), CHOOSECOLS(..., 2)), VSTACK("Nights", CHOOSECOLS(..., 3), CHOOSECOLS(..., 4)))

This results in:

MonthNights
01-12-20244
01-01-20251

The formula uses REDUCE to apply these calculations to each date pair, returning results in a vertically stacked format. It dynamically appends the start dates of each month to ensure each calculation is correctly linked to the corresponding month.

Summarizing these results will help us effectively Calculate Nights in Each Month in Excel.

Step 2: Summarize Nights in Each Month with SUMIF or a Pivot Table

Once the nights per month are calculated, you can summarize them using either a Pivot Table or a SUMIF formula.

Calculate Nights in Each Month Using a Pivot Table

Follow these steps to create a summary:

  1. Select the range D1:E17 (result of Step 1).
  2. Go to the Insert tab and click Pivot Table under the Tables group.
  3. Click OK to create a new worksheet with a Pivot Table layout.
  4. In the Pivot Table, check Month and Nights.
  5. If “Quarters” appear, uncheck them to focus on months only.

You’ll get a two-column summary showing the month and the corresponding number of nights.

Calculate Nights in Each Month Using a Pivot Table (Summary in Excel)

This is the simplest way to Calculate Nights in Each Month in Excel using a Pivot Table.

Calculate Nights in Each Month with SUMIF

If you prefer not to use a Pivot Table, you can use the SUMIF formula:

  1. In G2:G13, enter the month start dates for the year you want (e.g., 01/01/2025 to 01/12/2025).
  2. In H2, enter the following SUMIF formula and drag it down:
=SUMIF($D$2:$D$17, G2, $E$2:$E$17)
Calculate Nights in Each Month Using a Formula (Summary in Excel)

Alternatively, for a self-expanding formula, use:

=MAP(G2:G13, LAMBDA(val, SUMIF($D$2:$D$17, val, $E$2:$E$17)))

The SUMIF approach allows you to customize the month start dates and add extra columns for comments or other data.

Resources

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Expert Prashanth KV is a Diamond Product Expert in Google Sheets, officially recognized by Google for his contributions to the Docs Editors Help Community and featured in the Google Product Experts Directory. With years of experience working with Google Sheets and Excel for multinational firms in Mumbai and Dubai, he has been blogging since 2012, offering practical, real-world spreadsheet solutions that professionals rely on. Explore his blog to learn advanced formulas, automation tips, and problem-solving techniques to elevate your spreadsheet skills.

Mode of Comma-Separated Numbers in Excel (Dynamic Array)

There is no dedicated function in Excel to directly find the mode of comma-separated...

How to Find Mode of Comma-Separated Numbers in Google Sheets

Working with comma-separated numbers inside a single cell is a common scenario in Google...

How to Count Merged Cells in Google Sheets (and Get Their Size)

Sometimes, you may have blocks of merged cells in a column or row. But...

Count Consecutive Workday Absences in Google Sheets

This tutorial offers a powerful formula-based solution to count consecutive workday absences in Google...

More like this

Mode of Comma-Separated Numbers in Excel (Dynamic Array)

There is no dedicated function in Excel to directly find the mode of comma-separated...

Sort Each Row Individually in Excel Using a LAMBDA Formula

Sorting rows in Excel typically refers to rearranging entire datasets based on values in...

Sort by Field Labels Using the SORT and XMATCH Combo in Excel

Want to sort your Excel data by column names instead of column positions? Learn...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.