Calculate Excel Shift Hours Across Midnight
Question details
Calculate the total duration of two shifts when either shift may end after midnight.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- An employee's start and end times are entered in separate cells for two shifts.
- Observed behavior
- Subtracting an evening start time from an after-midnight end time can produce a negative value that Excel cannot display in its standard date system.
Check that D9 and E9 contain the first shift's start and end times, and G9 and H9 contain the second shift's times. Enter valid Excel times with AM or PM where appropriate; this method assumes each shift is shorter than 24 hours.
Use MOD to calculate both shift durations
Use this formula when one or both shifts may cross midnight and you need a combined duration.
Select I9, enter =MOD(E9-D9,1)+MOD(H9-G9,1), and press Enter.
Select I9 and open Format Cells with Ctrl+1 or the cell's right-click menu. Choose Custom and enter [h]:mm to show total hours, including totals over 24 hours.
If the result is incorrect, select the time cells and temporarily apply the General number format. Valid time values appear as numbers rather than time text; reenter any text values as times.

Calculate overnight shift totals in WPS Spreadsheets
Use the same MOD formula in WPS Spreadsheets to add two shifts that may cross midnight, then apply a duration format to show the total hours.
- 1. Open the spreadsheet: Open WPS Spreadsheets and use Open to select your Excel workbook.
- 2. Check the shift times: Enter the first shift's start and end times in D9 and E9, and the second shift's times in G9 and H9.
- 3. Calculate the total: Select I9, type =MOD(E9-D9,1)+MOD(H9-G9,1), and press Enter.
- 4. Show total hours: Select I9, open Format Cells from the right-click menu, choose Custom, and enter [h]:mm.

Frequently Asked Questions
How do I calculate just one shift that crosses midnight?
If the start time is in D9 and the end time is in E9, enter =MOD(E9-D9,1) in the result cell. Format the result as [h]:mm.
Why does my total reset after 24 hours?
A standard time format can display the time of day rather than the full duration. Open Format Cells, choose Custom, and use [h]:mm to display accumulated hours.
How can I tell if a clock-in value is stored as text?
Select the source cell and temporarily apply the General number format. A valid Excel time appears as a numeric value, while a text entry remains text.
Can this formula calculate a shift longer than 24 hours?
No. MOD with a divisor of 1 discards whole days. For longer shifts, enter complete start and end dates and times, subtract the start from the end, and format the result as [h]:mm.




