Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 280
Amol Avatar asked Dec 08 '17 05:12

Amol


People also ask

How do I convert ISO to timestamp?

Use the getTime() method to convert an ISO date to a timestamp, e.g. new Date(isoStr). getTime() . The getTime method returns the number of milliseconds since the Unix Epoch and always uses UTC for time representation. Copied!

How do I show a timestamp in Unix?

To find the unix current timestamp use the %s option in the date command. The %s option calculates unix timestamp by finding the number of seconds between the current date and unix epoch. You will get a different output if you run the above date command.

How do I get the current UNIX timestamp in python?

In the time module, the timegm function returns a Unix timestamp. The timetuple() function of the datetime class returns the datetime's properties as a named tuple. To obtain the Unix timestamp, use print(UTC).


1 Answers

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.
like image 105
asio_guy Avatar answered Sep 30 '22 09:09

asio_guy