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
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!
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With