Skip to main content

How to Calculate Hours for an Overnight Shift in Power Apps

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

A shift from 22:00 to 06:00 lasts eight hours, but on one day's clock 06:00 comes before 22:00, so a plain subtraction goes negative. When the finish time is earlier than the start, put it on the next day, then count the minutes between the two moments.

The example is the Overtime Timesheet app, where people log night shifts by choosing a date, a start time and a finish time.

The Rule​

  • The date people choose is the day the shift started.
  • If the finish time is earlier than the start time, the shift finished the next day.
  • If it's later, the shift finished the same day.

Equal times are a different matter: a shift from 22:00 to 22:00 is more likely a slip than 24 hours of work, so the example asks people to correct it (How to Validate a Form Without an Edit Form in Power Apps).

Prerequisites​

Step 1: Put the Finish on the Right Day​

The example keeps the rule in a user-defined function in App.Formulas (How to Create a User-Defined Function in Power Apps):

App → Formulas
ShiftEnd(ShiftDate: Date, StartAt: Time, EndAt: Time): DateTime =
With(
{EndDate: If(EndAt < StartAt, DateAdd(ShiftDate, 1, TimeUnit.Days), ShiftDate)},
DateTime(Year(EndDate), Month(EndDate), Day(EndDate), Hour(EndAt), Minute(EndAt), 0)
);
  • EndAt < StartAt compares the two times of day.
  • DateAdd moves the date on by one day when the finish is earlier.
  • DateTime puts the finish time on that day.

For a shift starting on 6 November at 22:00 and finishing at 06:00, the finish is the 7th:

Any text control → Text
Text(ShiftEnd(Date(2026, 11, 6), Time(22, 0, 0), Time(6, 0, 0)), "dd/mm/yyyy hh:mm")

The formula bar with Text(ShiftEnd(Date(2026, 11, 6), Time(22, 0, 0), Time(6, 0, 0)), "dd/mm/yyyy hh"), the preview strip reading 07/11/2026 06:00 with Data type text, and the text control showing the same

Step 2: Count the Minutes​

With the start built the same way, by a ShiftStart function, DateDiff counts the minutes between them:

App → Formulas
ShiftMinutes(ShiftDate: Date, StartAt: Time, EndAt: Time): Number =
DateDiff(ShiftStart(ShiftDate, StartAt), ShiftEnd(ShiftDate, StartAt, EndAt), TimeUnit.Minutes);

22:00 to 06:00 is 480 minutes:

The formula bar with ShiftMinutes(Date(2026, 11, 6), Time(22, 0, 0), Time(6, 0, 0)), the preview strip reading 480 with Data type number, and the text control showing 480

Step 3: Take Off the Break​

Subtract the unpaid break, in minutes, and divide by 60. In the app, with a 60-minute break, the screen shows 7.00 hours as soon as both times are chosen:

The Log overtime screen in Preview with the date 06/11/2026, Start 22:00, Finish 06:00, an unpaid break of 60 and Hours worked: 7.00 (7 h 0 min)

On the two nights a year the clocks change, the same shift is an hour longer or shorter in real time. Because the app counts between real moments, DateDiff gets those right too: How to Calculate Hours Across a Daylight Saving Time Change in Power Apps.

Common Mistakes​

MistakeWhat happensDo this instead
Putting the finish on the start date22:00 to 06:00 is minus 16 hoursMove it to the next day when it's earlier than the start
Comparing with <=Equal times become a 24-hour shiftUse < and treat equal times as a mistake
Counting with times only, no datesThe day the shift finished is lostBuild both moments with DateTime
Counting in TimeUnit.HoursPart hours are droppedCount minutes, then divide by 60

Troubleshooting​

SymptomLikely causeFix
Hours are negative for a night shiftThe finish is on the same day as the startUse the ShiftEnd rule
A daytime shift shows 24 or more hoursThe comparison put an equal or later finish on the next dayCompare EndAt < StartAt
The hours are 2 for a shift with no finishA blank finish counts as midnightCheck that both times are chosen first

FAQ​

What if a shift lasts more than 24 hours?​

Two times of day can't tell you that. The example refuses a shift longer than 24 hours with the message A shift can't be longer than 24 hours.; a longer stretch can be logged as two entries.

How does Excel do this?​

With MOD, which wraps a negative difference round the clock: How to Calculate Hours for an Overnight Shift in Excel with MOD.

Conclusion​

When the finish time is earlier than the start, the shift finished the next day: add a day with DateAdd, build the finish with DateTime, and count minutes with DateDiff. 22:00 to 06:00 is 480 minutes, or 7 hours after an hour's break.

Additional Resources​