Convert an ISO date to seconds since epoch in linux bash

后端 未结 2 1742
傲寒
傲寒 2020-12-17 16:18

I have a date in the ISO format YYYY-MM-DDTHH:SS (e.g. 2014-02-14T12:30). I\'d like to convert it in seconds since epoch using only the date command in linux bash.

A

相关标签:
2条回答
  • 2020-12-17 16:42

    With GNU date, specify the date to parse with -d and seconds since epoch with %s

    $ date -d"2014-02-14T12:30" +%s
    1392381000
    
    0 讨论(0)
  • 2020-12-17 16:55

    It is easier if you install gdate to deal with date strings that have timezones with nano second precision

    install coreutils and you will get gdate along

    on mac brew install coreutils

    gdate --date="2010-10-02T09:35:58.203Z" +%s%N
    

    This is particularly useful when inserting the time series value into influxdb

    in a shell script variable = $(gdate --date="2010-10-02T09:35:58.203Z" +%s%N)

    echo $variable
    
    0 讨论(0)
提交回复
热议问题