W4hf
W4hf

Reputation: 843

Convert specific date format to Epoch in linux

I need to convert this date format to epoch : 03/Apr/2016 14:22:59 the command

date -d "03/Apr/2016 14:22:59" +"%s"

will return :

date: invalid date ‘03/Apr/2016 14:22:59

Anyone can help me format it in a way it become recognizable by date -d ? Thanks in advance.

Upvotes: 1

Views: 921

Answers (3)

Lirt
Lirt

Reputation: 497

Info page for date input formats can be shown with following command:

info date "Date input formats"

Unfortunately your date format is not supported by date. You can however convert your string into format that is supported for example like this:

date -d "$(echo '03/Apr/2016 14:22:59' | tr -s "/" "-")" +"%s" 

To provide information about which kind of input strings can be used for date command I will write here a short summary:

Show date from epoch

date -d "@1513936964"

Words like today, tomorrow, month names (January), AM/PM, time zone names are supported

date -d "tomorrow 10:00:30PM"
date -d "03 April 2016 14:22:59"

Calendar formats

date -d "04/03/2016 14:22:59"
date -d "03-Apr-2016 14:22:59"
date -d "2016-04-03 14:22:59.00"

Timezones

date -d "PDT now"

Which day was Christmas last year?

date -d "$(date -d "2017-12-24") -1 year" +"%A"

Upvotes: 2

saeedgnu
saeedgnu

Reputation: 4366

Using Python:

python -c 'from time import strftime, strptime;print strftime("%s", strptime("03/Apr/2016 14:22:59", "%d/%b/%Y %H:%M:%S"))'

Upvotes: 1

choroba
choroba

Reputation: 241908

Perl to the rescue:

perl -MTime::Piece -e 'print Time::Piece
                             ->strptime("03/Apr/2016 14:22:59", "%d/%b/%Y %H:%M:%S")
                             ->epoch'

Upvotes: 2

Related Questions