1

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?

0

3 Answers 3

4
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
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

it returns date: invalid date '2014-06-02T16:23:13+02:00'
@MOHAMED, what does date --version tell you?
I tried with 2 linux the first one: date (GNU coreutils) 8.5 Copyright (C) 2010 Free Software Foundation, Inc. The second one is from BusyBox: BusyBox v1.19.4
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.