Posted in Bash Linux

Bash : converting unix timestamp to date

October 23, 2007 - 9 comments

Under Debian or RHEL4 and later, it can be as simple as :

$ date -d @1193144433
Tue Oct 23 15:00:33 CEST 2007

But that command doesn’t work under Redhat EL 3, so you should use the following :

$ date --date "1970-01-01 1193144433 sec" "+%Y-%m-%d %T"
2007-10-23 15:00:33

The output is different though
You can always use the previous command under Ubuntu/RHEL4+ or the simpler :

$ date -d @1193144433 "+%Y-%m-%d %T"
2007-10-23 15:00:33

Converting a date to unix timestamp :

$ date -d "2007-10-23 15:00:23" "+%s"
1193144423

Add "--utc" to the commands if you want to get the UTC time.

Comments

df

October 15, 2008 - 22:04

just what I needed! thanks!

zcharbi

November 4, 2008 - 1:24

Awesome. Works perfect.

Altair

December 3, 2008 - 15:01

Just simple and perfect! Exactly what I needed.

Xan

December 24, 2008 - 21:21

Thanks! That was very helpful. I’m just lazy for reading man’s))

I also prefer

date –d “2008-05-30 11:57:54″ +%s

for back converting

nicod

December 28, 2008 - 19:06

Merci beaucoup, pile poil ce qu’il me fallait.

misof

March 17, 2010 - 15:01

thanks

Johan

April 15, 2010 - 16:23

I want to do this vice versa, I want to convert a e.g.
2007-10-23 15:00:33
to unix timestamp in bash. How to do it?

patrick

April 15, 2010 - 21:54

Thanks for date -d @Ts
it save me a ton

Sébastien Wains

April 15, 2010 - 22:47

Johan,

# date -d “2007-10-23 15:00:23″ “+%s”
1193144423

I’m adding this to the post

Leave Comment

Please consider visiting the partners below if you enjoyed this article :

If this post saved you time and money, please consider checking my Amazon wishlist.

Before submitting, some rules :
- Is your comment related to the article ?
- You're having a problem ? Have you checked Google, other howtos, docs, manpages ?
- You're still having the problem ? Have you raised log verbosity, checked traces, ran tcpdump ?
- Have you checked your configuratoin for typo ?
Unless your comment is providing additional info or respect the rules above, DON'T comment.
If you don't understand what you are doing, I urge you to read the documentation, I'm not your free Level 1 helpdesk guy.