Time Duration Calculator: How to Calculate Hours Between Times [2026]

Calculating time durations sounds simple until the edge cases arrive: durations that cross midnight, time zones that shift mid-calculation, leap seconds, and the difference between wall-clock time and elapsed time. Whether you need to calculate shift hours for payroll, determine how long a process ran, or implement a countdown timer in code, understanding the underlying math prevents off-by-one errors that surface at 2 AM on a Tuesday.

This guide explains how time duration calculation works from first principles — the arithmetic, the edge cases, the standard formats, and how to implement accurate duration calculations in code.

1. What Is a Time Duration?

A time duration is the elapsed time between two specific points in time. It is a scalar quantity — a length of time without any reference to when it started or ended. "8 hours 30 minutes" is a duration. "From 9:00 AM to 5:30 PM" is a time interval. The duration is derived from the interval.

Duration and time of day are fundamentally different concepts:

Duration calculations subtract one timestamp or clock time from another. The result is always a non-negative span of time, expressed in whatever units are useful for the application.

2. How to Calculate Time Duration Manually

For same-day durations where both times are in 24-hour format, the calculation is straightforward subtraction:

Start time:  09:15  (9 hours, 15 minutes)
End time:    17:45  (17 hours, 45 minutes)

Step 1: Convert to total minutes since midnight
  Start: 9 × 60 + 15 = 555 minutes
  End:   17 × 60 + 45 = 1065 minutes

Step 2: Subtract
  1065 - 555 = 510 minutes

Step 3: Convert back to hours and minutes
  510 ÷ 60 = 8 remainder 30
  Result: 8 hours 30 minutes

For 12-hour (AM/PM) times, first convert to 24-hour format:

The special case for 12:xx AM and 12:xx PM trips people up: 12:30 AM is 00:30 (30 minutes after midnight), and 12:30 PM is 12:30 (30 minutes after noon). Treat 12 as 0 for the AM calculation.

3. Hours, Minutes, and Seconds: The Math

The base unit for precise duration calculations is seconds. Converting everything to seconds before operating eliminates the cascading error that comes from carrying over between time units:

Unit Seconds Minutes Hours
1 second 1 1/60 ≈ 0.0167 1/3600 ≈ 0.000278
1 minute 60 1 1/60 ≈ 0.0167
1 hour 3600 60 1
1 day 86400 1440 24

Converting Duration to Decimal Hours

Decimal hours are used for payroll, billing, and arithmetic operations on time. The formula:

decimal_hours = hours + (minutes / 60) + (seconds / 3600)

Examples:
  2h 30m       = 2 + 30/60          = 2.5 hours
  1h 45m 20s   = 1 + 45/60 + 20/3600 = 1.7556 hours
  8h 15m       = 8 + 15/60          = 8.25 hours

Converting Decimal Hours Back to Hours and Minutes

hours   = floor(decimal_hours)
minutes = round((decimal_hours - hours) * 60)

Example: 8.25 decimal hours
  hours   = floor(8.25) = 8
  minutes = (8.25 - 8) * 60 = 0.25 * 60 = 15
  Result: 8 hours 15 minutes

4. Handling Overnight Durations (Crossing Midnight)

An overnight shift that starts at 10:00 PM and ends at 6:00 AM the next day cannot be calculated with simple subtraction — the end time (6:00 = 360 minutes) is numerically less than the start time (22:00 = 1320 minutes).

The standard fix: if the end time in minutes is less than the start time in minutes, add 1440 (the number of minutes in a day) to the end time:

Start: 22:00 = 1320 minutes
End:   06:00 = 360 minutes

Since 360 < 1320, add 1440:
  Adjusted end = 360 + 1440 = 1800 minutes

Duration = 1800 - 1320 = 480 minutes = 8 hours

This approach assumes the duration spans at most one calendar day. For durations longer than 24 hours, you need full date-time values (not just clock times) to calculate correctly.

Multi-Day Durations

When your duration spans multiple days, work with Unix timestamps (seconds since epoch) or full datetime objects rather than clock times alone:

Start: 2026-04-14 22:00:00 UTC  →  Unix: 1744671600
End:   2026-04-16 06:00:00 UTC  →  Unix: 1744783200

Duration in seconds = 1744783200 - 1744671600 = 111600
Duration in hours   = 111600 / 3600 = 31 hours

Work with Timestamps Precisely

The SnapUtils Unix Timestamp Converter converts between Unix timestamps and human-readable dates instantly. Calculate exact durations, convert time zones, and verify timestamp arithmetic without writing a line of code.

Open Unix Timestamp Converter

5. Time Zones and Duration Calculations

Time zone handling is the source of the majority of bugs in time-based calculations. A duration measured in wall-clock time and a duration measured in elapsed time can differ significantly when time zones or daylight saving time (DST) transitions are involved.

The DST Transition Problem

When clocks spring forward by 1 hour (e.g., 2:00 AM becomes 3:00 AM), a shift that starts at 1:00 AM and ends at 5:00 AM appears to be 4 hours on the clock but is actually only 3 hours of elapsed time. When clocks fall back by 1 hour (3:00 AM becomes 2:00 AM), the same clock duration contains an extra hour of elapsed time.

The rule: never calculate durations using local wall-clock times if DST transitions may occur within the window. Convert all times to UTC first, subtract the UTC timestamps, and convert the result to hours and minutes. UTC never has DST transitions, so the subtraction is always accurate.

Cross-Time-Zone Durations

If a meeting starts at 9:00 AM in New York (UTC-5) and ends at 11:00 AM in London (UTC+0), the elapsed time is not 2 hours — it is actually 6 hours, because London is 5 hours ahead of New York. Converting both times to UTC resolves this:

9:00 AM New York (UTC-5)  = 14:00 UTC
11:00 AM London (UTC+0)   = 11:00 UTC

Duration in UTC = 11:00 - 14:00 = -3 hours???

In this case the London end time is earlier in UTC than the New York start time — meaning the meeting spans calendar days. Working with full ISO 8601 datetime strings (with explicit UTC offsets) makes cross-time-zone duration calculations unambiguous.

6. Duration Formatting Standards (ISO 8601)

ISO 8601 is the international standard for representing dates, times, and durations in a machine-readable format. The duration notation uses the pattern:

P[n]Y[n]M[n]DT[n]H[n]M[n]S

Breaking this down:

Real examples:

ISO 8601 Duration Human-Readable Meaning
PT8H30M 8 hours 30 minutes
PT90S 90 seconds
P1DT12H 1 day and 12 hours (36 hours total)
P1Y2M3DT4H5M6S 1 year, 2 months, 3 days, 4 hours, 5 minutes, 6 seconds
PT0.5S 500 milliseconds (half a second)

ISO 8601 durations are used in HTML5 <time> element datetime attributes, the JavaScript Temporal API, most server-side scheduling libraries, and calendar/scheduling data interchange formats.

7. Common Time Duration Formulas

Total seconds from HH:MM:SS

total_seconds = (hours * 3600) + (minutes * 60) + seconds

HH:MM:SS from total seconds

hours   = floor(total_seconds / 3600)
minutes = floor((total_seconds % 3600) / 60)
seconds = total_seconds % 60

Duration between two clock times (same day)

start_seconds = (start_h * 3600) + (start_m * 60) + start_s
end_seconds   = (end_h * 3600) + (end_m * 60) + end_s
duration      = end_seconds - start_seconds
if (duration < 0) duration += 86400  // handle overnight

Percentage of a work day completed

work_day_seconds = 8 * 3600  // 8 hours
elapsed_seconds  = current_time_seconds - start_time_seconds
percent_complete = (elapsed_seconds / work_day_seconds) * 100

8. Time Duration in Programming

Every major programming language provides abstractions for working with time durations. Using them is always preferable to manual arithmetic.

JavaScript

// Duration in milliseconds using Date objects
const start = new Date('2026-04-19T09:00:00Z');
const end   = new Date('2026-04-19T17:30:00Z');
const durationMs = end - start;  // milliseconds

const hours   = Math.floor(durationMs / 3600000);
const minutes = Math.floor((durationMs % 3600000) / 60000);
console.log(`${hours}h ${minutes}m`);  // 8h 30m

// Using Temporal API (modern JavaScript)
const duration = Temporal.Instant.from('2026-04-19T17:30:00Z')
  .since(Temporal.Instant.from('2026-04-19T09:00:00Z'));
console.log(duration.toString()); // PT8H30M

Python

from datetime import datetime, timezone

start = datetime(2026, 4, 19, 9, 0, 0, tzinfo=timezone.utc)
end   = datetime(2026, 4, 19, 17, 30, 0, tzinfo=timezone.utc)

duration = end - start  # timedelta object
total_seconds = duration.total_seconds()
hours, remainder = divmod(int(total_seconds), 3600)
minutes, seconds = divmod(remainder, 60)
print(f"{hours}h {minutes}m {seconds}s")  # 8h 30m 0s

Key Principles for Production Code

9. Frequently Asked Questions

How do I calculate the number of hours between two times?

Convert both times to minutes since midnight. Start: multiply the hour by 60 and add the minutes. End: same. Subtract start from end to get total minutes. Divide by 60 to get hours. If the result has a fractional part, multiply the fraction by 60 to get the remaining minutes. Example: 9:15 AM to 5:45 PM — start = 555 minutes, end = 1065 minutes, difference = 510 minutes = 8 hours 30 minutes. For overnight calculations, add 1440 to the end time if it is numerically less than the start time.

How do you calculate time duration that crosses midnight?

If the end time (in minutes since midnight) is less than the start time, add 1440 minutes to the end time before subtracting. Example: start 10:00 PM (1320 minutes), end 6:00 AM (360 minutes). Since 360 < 1320, adjusted end = 360 + 1440 = 1800. Duration = 1800 - 1320 = 480 minutes = 8 hours. For multi-day durations, use full datetime values and convert to Unix timestamps — subtract start from end in seconds, then divide by 3600 for hours.

What is ISO 8601 duration format?

ISO 8601 duration format uses the pattern P[n]Y[n]M[n]DT[n]H[n]M[n]S. The P prefix is mandatory and stands for "period". The T separator divides date components (years, months, days) from time components (hours, minutes, seconds). You only include the components you need: PT8H30M means 8 hours 30 minutes, P1DT12H means 1 day and 12 hours. This format is used in HTML5, the JavaScript Temporal API, and most modern scheduling and calendar systems.

How do I convert a duration to decimal hours?

Use the formula: decimal hours = hours + (minutes ÷ 60) + (seconds ÷ 3600). Examples: 2 hours 30 minutes = 2 + 30/60 = 2.5 hours. 1 hour 45 minutes = 1 + 45/60 = 1.75 hours. 8 hours 15 minutes 30 seconds = 8 + 15/60 + 30/3600 = 8.2583 hours. Decimal hours are used for payroll systems, invoicing, and any arithmetic involving time values — you cannot add or multiply time in HH:MM format directly, but you can with decimal hours.

Convert Timestamps and Calculate Durations Instantly

The SnapUtils Unix Timestamp Converter handles Unix timestamps, ISO 8601 datetimes, and time zone conversions with precision. No sign-up, no installation.

Open Timestamp Converter Free

Related guides: TOML Configuration Guide  •  YAML vs JSON  •  Hex Colors & WCAG Guide