简体   繁体   中英

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.

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'

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"

使用Python:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM