Calculate Overnight Shift Durations in Excel
Question details
The user needs to accurately calculate the duration of employee shifts that cross midnight to prevent errors in overlap and Gantt chart scheduling.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Managing a 24-hour employee schedule and building Gantt charts where shift times pass midnight.
- Observed behavior
- Standard subtraction results in negative numbers or errors when the shift ends on the following day.
Ensure your start and end times are correctly formatted as Time or Custom (h:mm AM/PM) in your spreadsheet before applying calculation formulas.
Calculate Overnight Shift Durations Using the IF Function
Use a logical IF formula to add a day to the calculation if the end time is earlier than the start time, properly handling midnight crossovers.
When a shift passes midnight, Excel interprets the end time (e.g., 2:00 AM) as smaller than the start time (e.g., 10:00 PM). This results in a negative duration, which breaks calculations and displays as '######'. By adding 1 (which equals 24 hours in Excel's time system) to the end time when it is strictly less than the start time, you can calculate the correct duration.
Click the cell where you want the total shift duration to appear.
Type the formula: =IF([@[End time:]]<[@[Start time]],1+[@[End time:]]-[@[Start time]],[@[End time:]]-[@[Start time]]). If you are not using an Excel Table, replace the structured references with standard cell references, such as =IF(C2<B2, 1+C2-B2, C2-B2).
Press Enter to execute the formula, then click and drag the fill handle down to calculate the durations for the remaining shifts.
Select the duration column, press Ctrl+1 to open Format Cells, and choose 'Time' or 'Custom' (e.g., h:mm) to display the calculated duration correctly.

Build a Stacked-Column Gantt Chart
Once the accurate shift durations are calculated, use them to create a visual Gantt chart representation.
Manage Employee Schedules Efficiently with WPS Spreadsheet
WPS Spreadsheet offers powerful time calculation functions, including IF and MOD, to accurately track employee shifts and build professional Gantt charts.
- 1. Format time columns: Open WPS Spreadsheet, highlight your start and end time columns, and set the cell format to 'Time'.
- 2. Apply time formula: Type the formula =IF(B2<A2, 1+B2-A2, B2-A2) or =MOD(B2-A2, 1) in your duration column to calculate hours worked.
- 3. Generate Gantt chart: Select the data, go to the Insert tab, and choose a Stacked Bar chart to visualize the project schedule or shifts.

Frequently Asked Questions
Why does my time calculation show '#########' in Excel?
Excel displays '#########' when a time calculation results in a negative number. This typically happens if you subtract a later start time from an earlier end time (such as an overnight shift) without adjusting the formula to account for the next day.
How do I calculate total hours worked in decimal format?
After calculating the time difference, multiply the formula result by 24 (e.g., =(End_Time - Start_Time)*24) and change the cell format from 'Time' to 'General' or 'Number'.
How do I identify overlapping shifts in Excel?
You can use conditional formatting or logical formulas like =AND(Start2 < End1, End2 > Start1). This formula checks if the start time of the second shift is earlier than the end time of the first shift, and if the end time of the second shift is later than the start time of the first shift.




