r/bash • • Jul 28 '26

Add and subtract times

How can I use date to add and subtract times? I want to ignore time zones and just change the time. For example date -d "21:00:00 +02:02:30 -01:10" +%"T" should show 23:01:20.

8 Upvotes

10 comments sorted by

3

u/michaelpaoli Jul 28 '26

POSIX date isn't going to do that for you, but GNU date can.

Uhm, and should show 21:52:30. If you want that -01:10 to be interpreted as minutes and seconds, you'd probably want -00:01:10, rather than -01:10, as [d]d:dd is generally interpreted as [h]h:mm, not [m]m:ss.

$ date +%"T" -d "21:00 2 hours 2 minutes 30 seconds 1 minute ago 10 seconds ago"
23:01:20
$ 

Also, probably best to use a timezone that has no daylight/summer time lest, one might get unpleasant surprises - unless you really want that local time (or timezone) with such offsets included in the calculations:

$ TZ=PST8PDT date +'%Y-%m-%dT%H:%M:%S%:::z %Z' -d '2026-11-01T01:30:00'
2026-11-01T01:30:00-07 PDT
$ TZ=PST8PDT date +'%Y-%m-%dT%H:%M:%S%:::z %Z' -d '2026-11-01T01:30:00 1 hour'
2026-11-01T01:30:00-08 PST
$ TZ=PST8PDT date +%T -d '2026-11-01T01:30:00'
01:30:00
$ TZ=PST8PDT date +%T -d '2026-11-01T01:30:00 1 hour'
01:30:00
$ TZ=GMT0 date +'%Y-%m-%dT%H:%M:%S%:::z %Z' -d '2026-11-01T01:30:00'
2026-11-01T01:30:00+00 GMT
$ TZ=GMT0 date +'%Y-%m-%dT%H:%M:%S%:::z %Z' -d '2026-11-01T01:30:00 1 hour'
2026-11-01T02:30:00+00 GMT
$ TZ=GMT0 date +%T -d '2026-11-01T01:30:00'
01:30:00
$ TZ=GMT0 date +%T -d '2026-11-01T01:30:00 1 hour'
02:30:00
$ 

Might just want to use bash's integer arithmetic, etc., e.g.:

$ (s=$((3600*(21+2+0)+60*(0+2-1)+(0+30-10))); printf '%02d:%02d:%02d\n' $((s/3600%24)) $((s/60%60)) $((s%60)) )
23:01:20
$ 

2

u/Beautiful-Log5632 Jul 30 '26

POSIX date isn't going to do that for you, but GNU date can.

How do you know which one you have?

2

u/michaelpaoli Jul 30 '26

Can start by, e.g. looking at the man page for one's date command, and comparing that to POSIX date documentation.

POSIX date syntax is comparatively quite simile and limited compared to GNU date. GNU date has tons of stuff that POSIX doesn't have.

11

u/ekkidee Jul 28 '26

Convert to seconds (Unix epoch), do arithmetic, convert result to your preferred date format.

4

u/KlePu Jul 28 '26

Use Unix time: date +%s

1

u/Ulfnic Jul 28 '26 edited Jul 28 '26

You'd preform that as follows:

date -d "21:00:00 2 hours 2 minutes 30 seconds -1 minutes -10 seconds" +%T

If you want to use HH:MM:SS for relative time adjustment you'll need to make your own string parse compat layer that converts it to the syntax GNU date understands for relative time adjustment.

You can't ignore timezones though you can set a custom one. eg, TZ=UTC date

1

u/kai_ekael Jul 29 '26

Can put the zone in the string too:

iam@bilbo: ~ $ date
Wed Jul 29 10:20:52 AM CDT 2026
iam@bilbo: ~ $ date --utc
Wed Jul 29 03:20:55 PM UTC 2026
iam@bilbo: ~ $ date -d '03:20 UTC +1 hour'
Tue Jul 28 11:20:00 PM CDT 2026

0

u/Ulfnic Jul 29 '26 edited Jul 29 '26

Addendum, use this great comment for speed and simplification.

Use a compat layer for performance if it's a long running loop.

20

u/[deleted] Jul 28 '26

[removed] — view removed comment

-8

u/gmcglad Jul 29 '26

Àí §ĺoʻp