Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a UNIX timestamp and I'd like to get a formatted date (like the output of date) corresponding to that timestamp.

My attempts so far:

$ date +%s
1282367908
$ date -d 1282367908
date: invalid date `1282367908'
$ date -d +1282367908
date: invalid date `+1282367908'
$ date +%s -d +1282367908
date: invalid date `+1282367908'

I'd like to be able to get output like:

$ TZ=UTC somecommand 1282368345
Sat Aug 21 05:25:45 UTC 2010
share|improve this question
add comment

4 Answers

up vote 18 down vote accepted

On Mac OS X and BSD:

$ date -r 1282368345
Sat Aug 21 07:25:45 CEST 2010
$ date -r 1282368345 +%Y-%m-%d
2010-08-21

with GNU core tools (you have to dig through the info file for that):

$ date -d @1282368345
Sat Aug 21 07:25:45 CEST 2010
$ date -d @1282368345 --rfc-3339=date
2010-08-21
share|improve this answer
    
It takes a lot of digging, though, which is surprising for such a common task. –  David Z Aug 21 '10 at 8:04
    
Thanks, accepted your answer. I should have read info coreutils date a little more carefully. –  Umang Aug 21 '10 at 8:27
add comment

After some googling, I found way to do it with the date command only:

$ date --date "Jan 1, 1970 00:00:00 +0000 + 1282367908 seconds"
Sat Aug 21 09:18:28 MSD 2010
share|improve this answer
    
Thanks for that. I was hoping there'd be something simpler, so just an up vote for now. –  Umang Aug 21 '10 at 6:36
    
another great example for how google makes you stupid. does nobody ever read the documentation anymore? –  hop Aug 21 '10 at 7:28
add comment

This perl one-liner will do it:

$ perl -e 'print scalar localtime $ARGV[0]' 1282367908
Sat Aug 21 09:18:28 2010
share|improve this answer
add comment

Another neat example of the rich heritage of modern Unix. This is indeed possible under most BSD variants:

$ TZ=UTC date -r 1282368345
Sat Aug 21 05:25:45 UTC 2010

(BTW your example seems to be off by one second)

share|improve this answer
    
Doesn't work on Linux. hop's answer seems to have the correct Linux answer. Thanks for pointing that out, I suspected something like that would happen, I've made it 45 seconds now. –  Umang Aug 21 '10 at 8:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.