I have a date in YYYY.MM.DD HH:SS format (e.g. 2014.02.14 13:30). I'd like to convert it in seconds since epoch using the date command.
The command
date -d"2014.02.14 13:30" +%s
won't work, because of the dots separation.
Any Ideas?
Why don't you make the date format acceptable? Just replace dots with dashes:
$ date --date="`echo '2014.02.14 13:30' | sed 's/\./-/g'`" +%s 1392370200 Here I first change the format:
$ echo '2014.02.14 13:30' | sed 's/\./-/g' 2014-02-14 13:30 and then use the result as a parameter for date.
Note that the result depends on your timezone.
awk using its gsub function.You can use:
s='2014.02.14 13:30' date -d "${s//./}" Fri Feb 14 13:30:00 EST 2014 To get EPOCH value:
date -d "${s//./}" '+%s' 1392402600 Perl: does not require you to munge the string
d="2014.02.14 13:30" epoch_time=$(perl -MTime::Piece -E 'say Time::Piece->strptime(shift, "%Y.%m.%d %H:%M")->epoch' "$d") echo $epoch_time 1392384600 Timezone: Canada/Eastern
-f