How to Use COUNTIFS for Time Ranges That Cross Midnight in Excel
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.

- 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).
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.
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.
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).
Click on the specific empty cell in your worksheet where you want the final count to be displayed.
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)
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 Standard COUNTIFS for Same-Day Intervals
If your specific time range falls entirely within the same calendar day (e.g., 8:00 AM to 5:00 PM), the standard COUNTIFS function remains the most efficient method.
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. Open your workbook: Launch WPS Office and open your spreadsheet document containing the time data.
- 2. Select the target cell: Click on the blank cell where you want to perform the time range calculation.
- 3. Input the formula: Type your =COUNTIF or =COUNTIFS formula into the formula bar just as you would in Microsoft Excel.
- 4. Process your data: Press the Enter key to instantly view your precise cross-midnight or same-day calculations.

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.




