Convert an ISO date to seconds since epoch in linux bash
With GNU date (from the GNU coreutils package), specify the date to parse with -d
and seconds since epoch with %s
$ date -d"2014-02-14T12:30" +%s
1392381000
Note that this will interpret the date to be parsed as being in your local time zone. If you want date
to use a specific time zone, you must specify that, either via the variable TZ (which changes the default time zone for date
), or in the date string. For UTC:
$ TZ=UTC date -d"2014-02-14T12:30" +%s
1392381000
or in the string, according to ISO 8601:
$ date -d"2014-02-14T12:30Z" +%s
1392381000
See ISO 8601 on Wikipedia for how to specify other time zones in the date string.
Commandline to convert ISO8601 time to unix timestamp
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.
Awk ISO to epoch
Try using getline
into a variable from a pipe instead of system
cmd="date +\%s -d "created; cmd | getline current_time
https://www.gnu.org/software/gawk/manual/html_node/Getline_002fVariable_002fPipe.html#Getline_002fVariable_002fPipe
The system
command returns the exit status returned by the command that was executed as its value.
http://www.delorie.com/gnu/docs/gawk/gawk_137.html
How do I convert a ISO 8601 timestamp to a Unix timestamp using Shell?
With the date
util on Unix-systems, you can do e.g.
$ date -d "2017-02-18T23:54:57+01:00" "+%s"
1285716915
The result is the Unix timestamp.
Convert timestamp to date results in time out of range error
Insert a comma:
date --iso-8601=ns -d @1606982602.015489365
Bash: date returns invalid date when I try to convert this date format to epoch
Linux supports the %d/%m/%Y
format on some locales for output but not for input. Shown as
LC_ALL="es_CR" locale -c d_fmt
LC_TIME
%d/%m/%Y
Fails for input
LC_ALL="es_CR" date -d '17/08/2021 21:45'
date: invalid date '17/08/2021 21:45'
For -d switch, US format is supported so your option is to reverse %d/%m
var=$(echo '17/08/2021 21:45' | sed -re 's@([0-9]{2,2})/([0-9]{2,2})/([^/]+)@\2/\1/\3@')
date -d "${var}" +"%s"
1629247500
Alternatively
IFS='/' read -r -a array <<< "17/08/2021 21:45"
date -d "${array[1]}/${array[0]}/${array[2]}" '+%s'
1629247500
Related Topics
Remove Empty Lines in a Text File via Grep
No Local Gulp Install Found Even After Installing Npm Install -G Gulp
Deleting a Folder from Svn Repository
Pass Output as an Argument for Cp in Bash
How to Remove the Win10's Path from Wsl
Iterate Over Lines Instead of Words in a for Loop of Shell Script
Changing Location of Core Dump
How to Change All Occurrences of a Word in All Files in a Directory
Http Debugging Proxy for Linux and MAC
Unix: Differencebetween Source and Export
Delete All System V Shared Memory and Semaphores on Unix-Like Systems
What's the Best Way to Distribute a Binary Application for Linux
Hard Time in Understanding Module_Device_Table(Usb, Id_Table) Usage
Using 'Find -Perm' to Find When a Permission Is Not Set
How to Find Files Modified in Last X Minutes (Find -Mmin Does Not Work as Expected)
How to Identify the Particular Linux Flavor via Command Line