logo
search
Function Problems

How to Use COUNTIFS for Time Ranges That Cross Midnight in Excel

Muhammad TalhaMuhammad Talha Oct 16, 2026 869 views

Question details

The user needs to count the number of data entries falling within a specific time shift or interval, but encounters errors when the time span crosses midnight.

How to Use COUNTIFS for Time Ranges That Cross Midnight in Excel
Product
Excel
Device & OS
not provided
Scenario
Calculating occurrences within a daily time interval that starts on one day and ends on the next (for example, a night shift from 10:00 PM to 2:00 AM).
Observed behavior
A standard COUNTIFS formula fails and returns zero for cross-midnight intervals because the mathematical underlying value of the end time (e.g., 2:00 AM) is numerically smaller than the start time (e.g., 10:00 PM).
Before you start

Ensure your data range contains strictly time values formatted correctly as Time or Custom (h:mm), rather than full Date-Time strings, before applying these specific formula workarounds.

Solution 1Recommended

Use a Dual COUNTIF Formula for Cross-Midnight Ranges

By splitting the condition into two separate COUNTIF formulas, you can independently handle the time segment before midnight and the time segment after midnight.

Since Excel evaluates times as fractions of a 24-hour day, a single AND condition (which COUNTIFS uses) cannot find a value that is simultaneously greater than 10:00 PM (0.916) AND less than 2:00 AM (0.083). Using two separate COUNTIF functions creates an OR condition.

1
Identify your data and criteria cells

Locate your main time data range (for example, cells $P$3:$P$22). Then identify the cell containing your start time (e.g., Q2) and the cell for your end time (e.g., R2).

2
Select the result cell

Click on the specific empty cell in your worksheet where you want the final count to be displayed.

3
Enter the dual COUNTIF formula

Click into the formula bar at the top and type the following formula exactly: =COUNTIF($P$3:$P$22,">="&Q2)+COUNTIF($P$3:$P$22,"<"&R2)

4
Calculate the result

Press the Enter key on your keyboard to execute the formula and calculate the total count of times falling within your cross-midnight interval.

Use a Dual COUNTIF Formula for Cross-Midnight Ranges
How it works: The first COUNTIF counts all times from your start time up to midnight (23:59:59). The second COUNTIF counts all times from midnight (0:00:00) up to your end time. Adding them together gives the correct total.
Efficient Spreadsheet Solution

Easily Manage Advanced Formulas with WPS Spreadsheet

WPS Spreadsheet offers complete support for advanced data calculation functions including COUNTIF and COUNTIFS, empowering you to seamlessly analyze complex time ranges and organize your datasets without hassle.

  1. 1. Open your workbook: Launch WPS Office and open your spreadsheet document containing the time data.
  2. 2. Select the target cell: Click on the blank cell where you want to perform the time range calculation.
  3. 3. Input the formula: Type your =COUNTIF or =COUNTIFS formula into the formula bar just as you would in Microsoft Excel.
  4. 4. Process your data: Press the Enter key to instantly view your precise cross-midnight or same-day calculations.
100% compatible with Microsoft Excel formulas, time values, and formattingLightweight software architecture for incredibly fast calculation speedsUser-friendly interface ideal for managing complex time-shift datasets
microsoft office alternative - wps office

Frequently Asked Questions

Why does a standard COUNTIFS formula fail when crossing midnight?

Excel stores time values as fractions of a 24-hour day. For instance, 10:00 PM is internally represented as 0.916, while 2:00 AM is 0.083. A normal COUNTIFS function requires all conditions to be met simultaneously (an AND logic). It asks Excel to find values that are greater than 0.916 AND less than 0.083 at the same time, which is mathematically impossible for a single time cell without a date attached.

Can I solve the cross-midnight issue by including dates with my times?

Yes. If your cells are formatted to contain full Date and Time values (e.g., "10/24/2023 22:00" and "10/25/2023 02:00"), the standard COUNTIFS function will work perfectly. Because the date is included, the underlying numerical value for the end time on the following day will be mathematically larger than the start time, eliminating the logic error.

How do I view the underlying numerical value of a time in Excel?

To see the actual decimal value that Excel uses for calculations, select the cell containing your time data, navigate to the 'Home' tab on the ribbon, locate the 'Number' group, and change the format dropdown from 'Time' to 'General'. This will reveal the fractional value between 0 and 1.