9

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

Amol
  • 1,084
  • 10
  • 20
  • The goal is that you add some code of your own to show at least the research effort you made to solve this yourself. – Cyrus Dec 08 '17 at 06:22

1 Answers1

14

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.
Community
  • 1
  • 1
asio_guy
  • 3,667
  • 2
  • 19
  • 35