Difference in date time

前端 未结 3 446
梦毁少年i
梦毁少年i 2021-01-24 10:21

I have 2 variables.

GMDCOMTM which stores the date time Tue Oct  1 13:32:40 2013
GMDRRSTM which stores the date time Tue Oct  2 23:35:33 2013

H

3条回答
  •  滥情空心
    2021-01-24 11:00

    Convert dates to %s ---> seconds since 1970-01-01 00:00:00 UTC.

    $ date -d"Tue Oct  2 23:35:33 2013" "+%s"
    1380749733
    

    So the thing is to get the difference in seconds between both dates using bc as calculator:

    $ d1="Tue Oct  1 13:32:40 2013"
    $ d2="Tue Oct  2 23:35:33 2013"
    $ echo $(date -d"$d2" "+%s") - $(date -d"$d1" "+%s") | bc
    122573
    

    Then you can get it into hours, days, with the great function Stéphane Gimenez indicates in UNIX & Linux:

    $ displaytime 122573
    1 days 10 hours 2 minutes and 53 seconds
    

提交回复
热议问题