Difference between two UTC datetime strings

368 views Asked by At

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.

1

There are 1 answers

0
Léa Gris On BEST ANSWER

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