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.

 

21 Comments

 

  1. October 3, 2007  6:52 am by artiomix Reply

    Thanks Mattias! It works :)

  2. October 5, 2007  11:55 am by Magnus Reply

    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.

  3. October 5, 2007  9:32 pm by artiomix Reply

    Hi Magnus,



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



    Thanks for comment!

  4. October 24, 2007  2:33 pm by Ivan Arsenijevic Reply

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

  5. October 24, 2007  7:30 pm by artiomix Reply

    Hi Ivan Arsenijevic,



    Thanks for you comment!

  6. November 29, 2007  2:09 pm by FDF Reply

    Great tips, thanks.

    FDF

  7. March 7, 2008  9:10 am by Deven Verma Reply

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

  8. March 13, 2008  1:17 am by Mike Reply

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

    • May 27, 2012  6:08 am by Sven Wendler Reply

      Try this tute on the difference on the date command on mac and linux
      http://sshadmincontrol.com/convert-simple-and-complex-dates-to-seconds-since-epoch-or-1970-on-a-mac-bsd-or-linux-using-bash

  9. August 18, 2008  3:53 pm by Kris Loranger Reply

    Mac OSX unix time stamp conversion is same as BSD

    date -r 1219073151

  10. August 5, 2009  12:03 pm by zsuffad Reply

    date -d @1249474436 works fine on archlinux, output:

    Wed Aug 5 14:13:56 CEST 2009

    ---------------

    thx for the tip

  11. August 7, 2009  5:16 pm by Matt Shields Reply

    Perfect. That saved me a bunch of time reading through the man pages. :)

  12. October 20, 2009  4:42 pm by Chaos Reply

    date +%s gives you unix-time

    date -d @123456890 converts back.



    simple way to test

    date -d @$(date +%s) (this basicaly takes the output from "date +%s" as input for "date -d@" and thus should print the current time in human readable form)

  13. February 22, 2010  3:39 pm by Borko Jankovic Reply

    Daj Ivane nemoj da kenjas!!! :)

  14. April 17, 2011  8:43 pm by Linda Walsh Reply

    scriptify! This will function as the 'date' command, but with first param being the "epoch seconds" (allowing all the date command reformatting options):
    ---
    #!/bin/bash
    s=$1
    shift
    date -d @$s "$@"

  15. April 23, 2011  6:57 pm by San Diego Local SEO Reply

    Hiya. Very nice website!! Man .. Beautiful .. Superb .. I will bookmark your blog and take the feeds also...I'm happy to locate a lot of useful info here in the article. Thank you for sharing...

  16. April 27, 2011  10:07 pm by jordan retro Reply

    Great – I should definitely say I'm impressed with your site. I had no trouble navigating through all tabs as well as related info. It ended up being truly easy to access. Good job.

  17. April 20, 2012  9:12 am by Alessandra Reply

    Thanks for the tips, very useful. Both forms work for me.

  18. September 25, 2012  9:03 am by Dennis Reply

    In FreeBSD convert from date to unixtime:

    date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"
    can be used to parse the output from date and express it in Epoch time.

    Or for any date:
    utm utm # date -j "201209252359.00" "+%s"
    1348603140
    utm utm # date -r 1348603140
    ???????, 25 ???????? 2012 ?. 23:59:00 (MSK)

Leave a reply

 

Your email address will not be published.