Search code examples
bashunix-timestamp

Commandline to convert ISO8601 time to unix timestamp


What is the most efficient way to get a unix timestamp for a given ISO 8601 date and vice versa?

There are several third party websites to do this. However I am looking for a simple command that can be executed at the Linux prompt


Solution

  • Convert ISO Date/Time to Unix timestamp

    date -d 'date' +"%s"
    

    Example

       bash-# date -d 'Fri Dec  8 00:12:50 UTC 2017' +"%s"
       bash-# 1512691970
    

    man -a date

       -d, --date=STRING
              display time described by STRING, not 'now'
    
       %s     seconds since 1970-01-01 00:00:00 UTC
    

    Convert Unix timestamp to ISO Date/Time

    date -Iseconds -d @<unix timestamp>
    

    Example

       bash-# date -Iseconds -d @1512711426
       bash-# 2017-12-07T21:37:06-0800
    

    man -a date

       -d, --date=STRING
              display time described by STRING, not 'now'
    
    
       -I[TIMESPEC], --iso-8601[=TIMESPEC]
              output  date/time  in  ISO 8601 format.  
              TIMESPEC='date' for date only (the default), 'hours',                             
              'minutes', 'seconds', or 'ns' for date and time to the 
              indicated precision.