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.
or do this
date -d @1187769064
Thanks Mattias! It works :)
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.
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!
Completely useful tips that saved me from unneeded scripting on numerous occasions
Hi Ivan Arsenijevic,
Thanks for you comment!
Great tips, thanks.
FDF
Thanks. Solved my problem. On slackware, date -u --date=".... works, not date -d @...
anyone care to take a crack at it for the Mac OSX? (i.e., the above commands all do not work on the Mac).
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
Mac OSX unix time stamp conversion is same as BSD
date -r 1219073151
date -d @1249474436 works fine on archlinux, output:
Wed Aug 5 14:13:56 CEST 2009
---------------
thx for the tip
Perfect. That saved me a bunch of time reading through the man pages. :)
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)
Daj Ivane nemoj da kenjas!!! :)
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 "$@"
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...
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.
I love your wp template, where do you obtain it through?
Thanks for the tips, very useful. Both forms work for me.
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)