Time Duration Calculator
Add or subtract hours, minutes, and seconds to calculate time durations.
About this tool
Time Duration Calculator adds or subtracts hours, minutes, and seconds for you, or works out the gap between a start time and an end time. It spares you the error-prone mental math of carrying seconds into minutes and minutes into hours when you just want a clean total.
Enter your time values and choose whether to add them together, subtract one from another, or measure the span between two clock times, and the result appears instantly. It is handy for filling in timesheets, trimming video and audio clips, tracking cooking and baking stages, logging workout splits, and tallying billable hours.
The calculator handles every carry-over on its own, rolling 90 seconds into 1 minute 30 seconds and 75 minutes into 1 hour 15 minutes without any rounding surprises. Everything runs entirely in your browser, so your numbers stay on your device and results are immediate even offline.
Frequently Asked Questions
Code Implementation
import re
def parse_duration(s: str) -> int:
"""Parse a duration string like '1h 30m 45s' into total seconds."""
pattern = r'(?:(d+)s*dw*)?s*(?:(d+)s*hw*)?s*(?:(d+)s*mw*)?s*(?:(d+)s*sw*)?'
match = re.fullmatch(pattern.strip(), s.strip(), re.IGNORECASE)
if not match or not any(match.groups()):
raise ValueError(f"Cannot parse duration: {s!r}")
d, h, m, sec = (int(x or 0) for x in match.groups())
return d * 86400 + h * 3600 + m * 60 + sec
def format_duration(seconds: int) -> str:
"""Format total seconds back to a human-readable string."""
seconds = abs(int(seconds))
d = seconds // 86400
h = (seconds % 86400) // 3600
m = (seconds % 3600) // 60
s = seconds % 60
parts = []
if d: parts.append(f"{d}d")
if h: parts.append(f"{h}h")
if m: parts.append(f"{m}m")
if s or not parts: parts.append(f"{s}s")
return " ".join(parts)
def add_durations(*durations: str) -> str:
total = sum(parse_duration(d) for d in durations)
return format_duration(total)
# Examples
print(parse_duration("1h 30m")) # 5400
print(parse_duration("2d 4h 15m 0s")) # 187500
print(format_duration(5400)) # "1h 30m"
print(add_durations("1h 30m", "45m", "2h 15m")) # "4h 30m"Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.