简体   繁体   中英

Converting time to unix timestamp

I have the following time format

"2014-06-02T16:23:13+02:00"

I want to convert it to the unix timestamp (since 1/1/1970). For the above example, The command should return:

1401725705

Are there a linux command for that?

date -d"2014-06-02T16:23:13+02:00" +%s 

should work if you have a newer GNU coreutil. I tested with v8.22 latest.

if you are on a elder coreutil box like 8.4. it won't work, you could try replace the T with space. use string "2014-06-02 16:23:13+02:00" in date -d"2014-06-02 16:23:13+02:00" +%s

this is tested on Red Hat Enterprise Linux Server release 6.5 (Santiago) with coreutil 8.4

$>date -d"2014-06-02 16:23:13+02:00" +%s                                    
1401718993
date -d"2014-06-02T16:23:13+02:00" +%s

As pacholik indicates, %s is the way to convert date to seconds since 1970-01-01 00:00:00 UTC.

You can try either of these (note the usage of -d or --date= , as well as %s with + in or outside the quotes:

$ date --date="2014-06-02T16:23:13+02:00" "+%s"
1401718993

$ date --date="2014-06-02T16:23:13+02:00" +"%s"
1401718993

$ date -d"2014-06-02T16:23:13+02:00" +"%s"
1401718993

As a side note, you can do the opposite with -d@TIME :

$ date -d@1401718993
Mon Jun  2 14:23:13 UTC 2014

I see you were having problems with the T - time designator :

$ date -d"$(sed 's/\(.*\)T\(.*\).*/\1 \2/' <<< "2014-06-02T16:23:13+02:00")" +"%s"
1401718993

This sed gets the string and removes the T in the middle.

$ sed 's/\(.*\)T\(.*\).*/\1 \2/' <<< "2014-06-02T16:23:13+02:00"
2014-06-02 16:23:13+02:00

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