Two-way conversion of Unix time (seconds since 1970) and regular time

Welcome to Linux Screw! If you're new here, you may want to subscribe our RSS feed.

I found using of Unix time to be very useful in various shell scripts and here are two simple commands to convert Unix/Linux date command to Unix time format and back to regular formating:

To convert Unix time to simple (regular) time please use:

date -u --date="1970-01-01 1187769064 sec GMT"

where 1187769064 is input Unix time. The output will be: Wed Aug 22 07:51:04 UTC 2007

To get Unix time seconds from regular one format just use:

date --date="Wed Aug 22 07:51:04 UTC 2007" +%s

where Wed Aug 22 07:51:04 UTC 2007 is input regular time. The output will be: 1187769064.

Update: another way to convert Unix time into regular date is to use the following command date -d @1187769064 (thanks to Mattias Lindvall), that is tested in Ubuntu and Fedora.

 
 
» You might also be interested in the following articles:
Manage gzip files on-the-fly (zcommands)
FAQ: How to find out CPU load in Linux?
Summarize system resourses used by Linux application
FAQ: Iptables rules on timely basis
Ping range of IP addresses in parallel with fping



» Want to stay up to date? Subscribe to our E-MAIL or RSS feed!


10 Responses to “Two-way conversion of Unix time (seconds since 1970) and regular time”


  1. 1 Mattias Lindvall

    or do this
    date -d @1187769064

  2. 2 artiomix

    Thanks Mattias! It works :)

  3. 3 Magnus

    date -d @1187769064 doesn’t do it for me. I get “invalid date” running on Debian Sarge.

    date -u –date=”1970-01-01 1187769064 sec GMT” works fine.

  4. 4 artiomix

    Hi Magnus,

    Hm… I’ve tested date -d @1187769064 in Ubuntu and Fedora without errors… I’ve got error date: invalid date only after I’ve tried more than 11 digits (11877690644 instead of 1187769064). Please check input one more time ;)

    Thanks for comment!

  5. 5 Ivan Arsenijevic

    Completely useful tips that saved me from unneeded scripting on numerous occasions

  6. 6 artiomix

    Hi Ivan Arsenijevic,

    Thanks for you comment!

  7. 7 FDF

    Great tips, thanks.
    FDF

  8. 8 Deven Verma

    Thanks. Solved my problem. On slackware, date -u –date=”…. works, not date -d @…

  9. 9 Mike

    anyone care to take a crack at it for the Mac OSX? (i.e., the above commands all do not work on the Mac).

  10. 10 Kris Loranger

    Mac OSX unix time stamp conversion is same as BSD
    date -r 1219073151

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word




Friendly Sites:Who is behind Linux Screw?
Aspiring Sysadmin | GeekyBits³ | Bash Cures Cancer | TOTMS
Linux Operating System | Small Linux Deployments | My SysAd Blog
The Danesh Project | ZEPY | Linux config Wiki | Planet Sysadmin
The Sys Admin | {buhay sysad} | a non-geek's linux notes
Linux HOWTOs, Tutorials & Projects with Adam Palmer | LinuxAlt.Com
My name is Artem Nosulchik (artiomix AT gmail DOT com) and I'm Linux/Unix, Cisco systems engineer. The main idea of Linux Screw is to share relevant knowledge, skills and observations over The Web. Here you can find a lot of information related to different Linux distributions, FreeBSD, IOS as well as a other Open Source around staff. Read more ››