I have a date string in the following format Jul 27 23:59:59 2016 GMT
and I need to convert it to the equivalent epoch timestamp with the OS X BSD date
command.
GNU date
has a nice -d
/--date=STRING
argument:
$ date -d "Jul 27 23:59:59 2016 GMT" +'%s'
1469663999
The BSD date
command on OSX sadly has no such option.
date -j -f "<FORMAT>" "Jul 27 23:59:59 2016 GMT" +'%s'
seems to be the way to go, but I can't find the write format string. Apple's man page states:
date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"
can be used to parse the output from date and express it in Epoch time.
But that doesn't appear to be true:
$ date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"
Failed conversion of ``Wed 13 Jul 2016 11:17:49 BST'' using format ``%a %b %d %T %Z %Y''
date: illegal time format
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
How can I convert a date string in this format to an epoch timestamp with the OS X BSD date
command?
I can't seem to get a version out of date
, but I'm on OS X 10.11.5
(El Capitan) if that's significant.
Your
date
command is outputting the date in the following format:and you're trying to parse it with an expression to match the following format:
Resulting in:
So, basically, you need to change the format sequence in your command with:
In order to match the
Wed 13 Jul 2016 11:17:49 BST
format that yourdate
command is outputting by default.To use a custom date based on the same format:
Some references on what [some of] the format string sequences mean:
Do you mean this?
I worked that out by telling
date
to output in the same format as you were using:Here's another way:
I ran into an issue trying to convert a similar
datetime
toepoch
. Thought I would put this out here for this format as well.date -j -f "%Y-%m-%d %H:%M:%S" "2018-01-30 15:58:50" "+%s"
1517349530