Time difference in Excel and Google Sheets, even past midnight
You type 22:00 in A2, 06:00 in B2, enter =B2-A2, and Excel fills the cell with #####. Nothing is broken: the spreadsheet just did not know the shift ended the next morning. One function fixes it, and once you see why, every other time formula follows.
How spreadsheets store time
Excel and Google Sheets store a time as a fraction of one day. 06:00 is 0.25, 12:00 is 0.5, 18:00 is 0.75. One hour is 1/24 ≈ 0.041667 and one minute is 1/1440 ≈ 0.000694. A date is a whole number of days, so a date with a time is simply a whole number plus a fraction.
Three consequences follow. Subtracting two times gives a fraction of a day. Multiplying that fraction by 24 gives hours. And a result below zero has no clock time to show, which is where the trouble starts.
The basic subtraction
For a shift within one day, =B2-A2 is all you need. With 9:15 in A2 and 17:00 in B2 the result is 0.322917, which the h:mm format displays as 7:45. If the cell shows a decimal like 0.32, change its format to time (h:mm). Enter times with a colon (9:15, or 9:15 AM) so the spreadsheet recognises them as times and not text.
Past midnight: =MOD(B2-A2,1)
With 22:00 in A2 and 06:00 in B2, B2-A2 is 0.25 − 0.916667 = −0.666667. Excel's default date system cannot display a negative time and shows #####; Google Sheets keeps the negative value. Either way, the number is not the shift.
MOD returns the remainder after division, and its result has the same sign as the divisor (Microsoft's MOD reference; Google Sheets has the same MOD function). Microsoft gives it as MOD(n, d) = n − d × INT(n/d). With d = 1:
So =MOD(B2-A2,1) adds one day when the result is negative and does nothing when it is already positive. It is safe to use on every row, day shifts included. The one case it cannot handle is a shift of 24 hours or longer: MOD throws the whole days away. For those, use dates (below).
Decimal hours for payroll
Multiply by 24: =MOD(B2-A2,1)*24 turns 0.333333 into 8. Format that cell as a number, not as time, or it will show 0:00. Because day fractions are rarely exact, I wrap it in ROUND for anything that feeds pay: =ROUND(MOD(B2-A2,1)*24,2). Google's help page itself warns that MOD on non-whole numbers can give results that are slightly off and suggests ROUND.
A worked row: A2 7:58, B2 16:07, so the span is 8 h 9 min = 8.15 decimal hours. Decimal hours to h:mm goes the other way: divide by 24 and format as h:mm. 7.65/24 shown as h:mm is 7:39.
Taking off a break
Keep the unpaid break in minutes in its own column, say C2 = 30. Minutes become a day fraction when divided by 1,440:
For 7:58 to 16:07 with 30 minutes off, the first formula shows 7:39 and the second 7.65. A common mistake is typing 30 into a time-formatted cell: the spreadsheet reads that as 30 days. Either keep the break as plain minutes and divide by 1,440, or enter it as 0:30.
Totals over 24 hours: [h]:mm
Add a week of shifts with =SUM(D2:D8) and a 39 h 30 min week may display as 15:30. The value is right (1.645833 days); the format is not. The plain h:mm format shows the time of day and drops whole days. Use the custom format [h]:mm in Excel, where the square brackets let hours run past 24. In Google Sheets choose Format → Number → Duration. For a decimal total, sum the decimal column or multiply the h:mm total by 24, then round once.
Dates and times in one cell
For spans longer than a day (a job that runs from Friday evening to Monday morning, a 36-hour shift), enter full date-times such as 13/07 18:00 in each cell. Then plain =(B2-A2)*24 gives hours with no MOD, because the dates already tell the spreadsheet which day is which. One thing a spreadsheet will not do: it ignores daylight saving. A 22:00–06:00 night over the fall-back change shows 8 hours, even though 9 real hours passed. When the clock change matters, use the Dates & times tab of the time duration calculator with a time zone selected.
Errors and odd results, decoded
- ##### in Excel: a negative time, or a column too narrow. Widen the column first; if it stays, wrap the formula in MOD.
- #VALUE!: one of the cells is text, often a time typed with a dot (9.15) or with a stray space. Retype it with a colon.
- 0:00 where you expect 8: a decimal result in a time-formatted cell. Format it as a number.
- 8.0000001 or 7.6499999: floating-point leftovers from day fractions. ROUND to 2 places.
- A total that is exactly 24 hours short: the h:mm format again; switch to [h]:mm.
Formula cheat sheet
| Task | Formula | Example |
|---|---|---|
| Time between two times, same day | =B2-A2 | 9:15 → 17:00 gives 7:45 |
| Time over midnight | =MOD(B2-A2,1) | 22:00 → 06:00 gives 8:00 |
| Decimal hours | =MOD(B2-A2,1)*24 | 8 |
| Minus break minutes in C2 | =MOD(B2-A2,1)-C2/1440 | 7:58 → 16:07, 30 gives 7:39 |
| Decimal hours minus break | =ROUND((MOD(B2-A2,1)-C2/1440)*24,2) | 7.65 |
| Dates and times in one cell | =(B2-A2)*24 | hours across days |
| Weekly total | =SUM(D2:D8) with format [h]:mm | 39:30 |
Build a weekly sheet step by step
- Columns: A Date, B Clock in, C Clock out, D Break (min), E Hours (h:mm), F Decimal hours.
- Format B and C as time (h:mm or h:mm AM/PM), D as a number.
- In E2: =MOD(C2-B2,1)-D2/1440, formatted as [h]:mm. Copy it down.
- In F2: =ROUND(E2*24,2). Copy it down.
- Under the rows: =SUM(E2:E8) formatted as [h]:mm, and =ROUND(SUM(E2:E8)*24,2) for the decimal total. Convert the summed time, not the rounded column.
- Check one row against the calculator's Timesheet tab, or paste its Download CSV output next to your sheet.
With four 8:00–16:30 days and 30-minute breaks plus one 22:00–06:15 night with a 45-minute break, the sheet should show 39:30 and 39.5.
Questions and answers
Why does Excel show ##### for a time difference?
The result is negative, usually because the end time is after midnight. Use =MOD(B2-A2,1) so the end counts as the next day.
How do I get decimal hours from a time in Excel?
Multiply by 24 and format the cell as a number: =MOD(B2-A2,1)*24. Wrap it in ROUND(…,2) for payroll.
Why does my weekly total show 15:30 instead of 39:30?
The h:mm format drops whole days. Use the custom format [h]:mm in Excel or Duration in Google Sheets.
Does Excel handle daylight saving time?
No. Spreadsheet times have no time zone, so a night across a clock change is off by an hour.