時間計算機
時間・分・秒を加算または減算して所要時間を計算。
このツールについて
時間計算ツールは、時・分・秒の足し算や引き算を代わりに行い、開始時刻と終了時刻の差も求めます。きれいな合計が欲しいだけなのに、秒を分へ、分を時へ繰り上げる面倒で間違えやすい暗算から解放してくれます。
時間の値を入力し、合計するか、一方から差し引くか、2つの時刻の間隔を測るかを選ぶだけで、結果がすぐに表示されます。勤務時間表の記入、動画や音声の切り出し、料理や製菓の工程管理、トレーニングのラップ記録、請求対象時間の集計などに便利です。
このツールは繰り上がりをすべて自動で処理し、90秒を1分30秒に、75分を1時間15分へと、丸め誤差の心配なく変換します。すべてブラウザー内で動作するため、入力した数値は端末内に留まり、オフラインでも結果は瞬時に得られます。
よくある質問
コード実装
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.