oyeesh
oyeesh

Reputation: 581

Difference between two UTC datetime strings

I have two UTC DateTime strings that I get from GitHub API. The format looks like 2020-09-20T23:25:51Z.

Could someone help me calculate the number of seconds between any given 2 DateTime strings? I can use bash or python 2.7.

Upvotes: 0

Views: 373

Answers (1)

Léa Gris
Léa Gris

Reputation: 19665

This date format is called ISO-8601

To compute the difference between dates in seconds, you simply convert date to seconds. In shell it is:

date -d '2020-09-20T23:25:51Z' +%s'

When you have two dates converted to seconds, it becomes a simple arithmetic problem:

sec1=$(date -d '2020-09-20T23:25:51Z' +%s)
sec2=$(date -d '2020-09-20T22:25:51Z' +%s)
secdiff=$((sec1 - sec2))
echo "$secdiff"

Here exactly 3600 seconds

See also: Shell script to get difference in two dates

Upvotes: 1

Related Questions