Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I often find myself needing to convert from one type of time to another, and haven't found a way I'm consistently happy with. Every time I do a quick google search, I come up with different (sometimes painfully complex) answers.

I'm happy with

date -d "Sep 5 1986" "+%s"

for converting a standard date to epoch time, but I can't seem to make the reverse work (and the man pages I've browsed aren't helping much.)

date -d "526276800" "+%m/%d/%y"

yields an error on my RHEL 5 install.

Can the community offer me some strategies for easily converting back and forth? I'll take all forms: awk, perl, date. What are the pros and cons of each method?

share|improve this question
up vote 6 down vote accepted

date -d @526276800 "+%m/%d/%y"

share|improve this answer
Ah ha! That's the notation I was looking for, thanks! I can't believe this didn't come up for me in my google searches. – Jax May 29 '09 at 15:20
I can't believe it's not in the man page for date. – dwc May 29 '09 at 16:56
I wondered that myself -- after a quick look at the bottom of the man page, I noticed that (at least on my RHEL 5 system) there is further documentation available in the info pages and the syntax is listed there (though I did have to follow something like 4 links to get there) – Jax May 29 '09 at 19:06
For the Red Hat centered here, like myself: this works from RHEL4 and up. The date command in RHEL3 is of the 4.x branch, which does not have this functionality. – wzzrd Jun 3 '09 at 12:24

On my Mac OS X box, I use:

date -r 526276800 +%m/%d/%y
share|improve this answer
This is on at least Mac OS X and OpenBSD. May be present on other BSD derived systems, too. – dwc May 29 '09 at 16:57

With GNU date:

date -d '1970-01-01 946684800 seconds'

share|improve this answer
That's a nice trick! – Jax May 29 '09 at 19:07

Or in Ruby:

% ruby -e 'puts'

% ruby -e 'puts'

% ruby -e 'puts 1243620270'
Fri May 29 11:04:30 -0700 2009

% ruby -e 'puts "at %I:%M%p"'
at 11:04AM

Here are the strftime formats, taken from the ruby Time class documentation:

  %a - The abbreviated weekday name (``Sun'')
  %A - The  full  weekday  name (``Sunday'')
  %b - The abbreviated month name (``Jan'')
  %B - The  full  month  name (``January'')
  %c - The preferred local date and time representation
  %d - Day of the month (01..31)
  %H - Hour of the day, 24-hour clock (00..23)
  %I - Hour of the day, 12-hour clock (01..12)
  %j - Day of the year (001..366)
  %m - Month of the year (01..12)
  %M - Minute of the hour (00..59)
  %p - Meridian indicator (``AM''  or  ``PM'')
  %S - Second of the minute (00..60)
  %U - Week  number  of the current year,
          starting with the first Sunday as the first
          day of the first week (00..53)
  %W - Week  number  of the current year,
          starting with the first Monday as the first
          day of the first week (00..53)
  %w - Day of the week (Sunday is 0, 0..6)
  %x - Preferred representation for the date alone, no time
  %X - Preferred representation for the time alone, no date
  %y - Year without a century (00..99)
  %Y - Year with century
  %Z - Time zone name
  %% - Literal ``%'' character
share|improve this answer
$ perl -le 'print scalar localtime 1243620900'
Fri May 29 11:15:00 2009
share|improve this answer

It may be a little heavy duty, but in perl there's a great module called DateTime that'll convert back and forth between a myriad of date & time formats, including epoch. It's very simple to use:

share|improve this answer

Personally, I alternatively use Python and bash for this. But since you are annoyed with it ;-) I'd go for making a small script that takes a date as a parameter and write it so as to have --2epoch and --2normal options.

That way, you can convert from epoch to normal with

./ --2normal 1243609635

and the other way around with

./script --2epoch "20090529 17:07"

Is the input date always formatted the same way? That would make the script a lot easier to write.

share|improve this answer

Perl one-liner:

$ perl -MDate::Manip -le 'print UnixDate(ParseDateString("epoch ".shift()),"%c")' 946684800

share|improve this answer

FROM epoch:

dump epoch timestamps to readable forms from stdin (first example) or as args (second example), and dont forget the power of strftime!

perl -pe '$_=~s/(\d{10})/scalar localtime($1)/eg'

perl -e '$s=join(" ",@ARGV);$s=~s/(\d{10})/scalar localtime($1)/eg;print"$s\n"'

perl -pe 'use POSIX qw(strftime);s/(\d{10,10})/strftime("%Y%m%d-%H:%M:%S",$1)/eg'

TO epoch:

If you have Date::Manip module installed for perl (which usually ISNT there by default), you can use:

perl -MDate::Manip -e '$stamp=UnixDate(ParseDate(join(" ",@ARGV)),"%s");printf"%d %s\n",$stamp,scalar localtime($stamp)' sep 9 2006

Sometimes it's hard to decide if you want to REPLACE the timestamps in your 'filter', or if you want to preface the line with the timestamp found. I flip back-n-forth on that one.

share|improve this answer

Your Answer


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.