Calculate Overnight Hours Between Midnight and 6 AM in Excel
Question details
The user needs to calculate the specific portion of a timesheet shift that was worked overnight between midnight and 6:00 AM.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Tracking employee shifts on a timesheet and isolating the total hours worked during a specific overnight window (12:00 AM to 6:00 AM).
- Observed behavior
- Users need to extract accurate time durations from date/time cells using mathematical formulas rather than formatting the values as text, which breaks downstream calculations.
Ensure that your start and end times are recorded as standard Excel Date/Time serial numbers (e.g., '10/24/2023 22:00') rather than plain text, so mathematical formulas can process them properly.
Use MIN and MAX Formulas for Shifts Up to 18 Hours
This is the most reliable method for standard work shifts. It uses mathematical logic to extract the time portion falling strictly between 0:00 (midnight) and 6:00 AM (0.25 as a daily fraction).
Avoid using the TEXT function to calculate durations, as it converts numeric time values into text strings that cannot be summed later. Instead, rely on mathematical extraction.
In Excel, times are stored as fractions of a day. Therefore, 6:00 AM is exactly 0.25 (6 divided by 24).
If you need to separate your date and time for visual reference, use =INT(H2) to get the whole date, and =MOD(H2,1) to extract just the fractional time.
Assuming your Start Time is in cell H2 and your End Time is in cell I2, click the cell where you want the overnight hours displayed and enter: =MAX(MIN(MOD(I2,1),0.25)-MOD(H2,1)*(INT(H2)=INT(I2)),0)
Right-click the result cell, select 'Format Cells', navigate to the 'Custom' category, and type [h]:mm into the Type box. This ensures durations display correctly even if they exceed 24 hours.

Calculate Extended Shifts Crossing Midnight
Use this expanded formula if an employee's shift is longer than 18 hours or spans across multiple consecutive days.
Create a Custom LAMBDA Function for Overnight Hours
If you frequently need to calculate overnight hours across many sheets, defining a reusable LAMBDA function allows you to use a simple name like =DownTime(start, stop).
Use WPS Spreadsheet for Advanced Time Calculations
WPS Spreadsheet fully supports Excel's complex time calculation formulas, including INT, MOD, and advanced logical arrays. It provides a lightweight, highly compatible environment to handle your timesheet data efficiently.
- 1. Open your timesheet: Launch WPS Spreadsheet and open your existing timesheet document.
- 2. Verify date/time formats: Select your Start and End columns, right-click to choose 'Format Cells', and ensure they are set to a Date/Time format rather than Text.
- 3. Apply the calculation formula: Enter the formula =MAX(MIN(MOD(I2,1),0.25)-MOD(H2,1)*(INT(H2)=INT(I2)),0) exactly as you would in Excel to calculate the midnight to 6 AM window.
- 4. Format elapsed time: Press Ctrl+1 to open the format menu, choose 'Custom', and input [h]:mm to correctly display the calculated durations.

Frequently Asked Questions
Why shouldn't I use the TEXT function to calculate time differences?
The TEXT function converts numeric date and time values into text strings. While it might look correct visually (e.g., '04:00'), a text string cannot be processed by mathematical formulas like SUM. If you try to add up a week's worth of text-based hours, you will get an error or a result of zero.
What does the [h]:mm custom format do?
In spreadsheet software, standard 'h:mm' formatting resets to zero once it hits 24 hours. Placing square brackets around the 'h' ([h]:mm) tells the program to display total elapsed hours, allowing you to correctly show totals like '32:15' for a 32-hour work week.
Why is the MOD function used for time calculations?
Dates and times are stored as serial numbers where whole numbers represent days and decimals represent time (e.g., 0.5 is 12:00 PM). Using =MOD(cell, 1) divides the serial number by 1 and returns only the remainder, effectively stripping away the date and leaving only the pure time value for calculation.
Why is 6:00 AM represented as 0.25 in the formula?
Because time is calculated as a fraction of a 24-hour day. 6 divided by 24 equals 0.25. Similarly, 12:00 PM (noon) is 0.5, and 6:00 PM is 0.75.




