Search code examples
bashdatedate-arithmeticiso8601

Time difference in hours between 2 ISO - 8601 compliant dates


I have 2 dates that are both in ISO - 8061 compliant date format.

echo $START
2019-02-14T16:09:13Z

echo $END
2019-02-14T19:43:12Z

Now I want to find the time difference between both. I would prefer the difference to be in hours.

What Ive tried is :

echo "$(($(date -d "$START" '+%s') - $(date -d "$END" '+%s')))"

But it does not work. Any ideas whats the best way to solve this ?


Solution

  • I'd use the dateutils tools.

    Then the answer becomes:

    > datediff 2019-02-14T16:09:13Z 2019-02-14T19:43:12Z -f "%H hours"
    3 hours