Convert unix timestamp to readable date

Unix time, or POSIX time, is a system for describing instants in time, defined as the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of Thursday, January 1, 1970 (Unix times are defined, but negative before that date), not counting leap seconds. It is used widely in Unix-like and many other operating systems and file formats.

As a unix time is an large integer, we will show how to convert it into human readable format in different ways.

Convert a timestamp by Shell command

$ date -d @1335667666
Sun Apr 29 10:47:46 CST 2012

You can always use the previous command under a Linux:

$ date -d @1335667666 "+%Y-%m-%d %T"
2012-04-29 10:47:46

Accordingly, you can convert a date to unix timestamp as below:

$ date -d "2012-04-29 10:47:46" "+%s"

Convert a timestamp by Perl
Well, it’s just one liner as below:

$ perl -e 'print scalar localtime (1335667666),"\n"'
Sun Apr 29 10:47:46 2012

Other ways to Convert a Unix timestamp
1, PHP

date('d.m.Y H:i:s', 1335667666);

2, MySQL

select from_unixtime(1335667666);

3, JavaScript

new Date(1335667666*1000).toString()

4, C++

time_t epch =1335667666;
printf("%i -> %s", epch, asctime(gmtime(&epch))); (time.h)

5, Java

new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new Date(1335667666* 1000L))

6, gawk
To convert this timestamp into something readable, copy below two lines into a small script “time-convert.gawk” and make it executable.

#!/bin/gawk -f
{ print strftime("%c", $0); }

Then call the tool with the following command:

$ echo 1335667666 | ./time-convert.gawk
Sun 29 Apr 2012 10:47:46 AM CST

Share this post

2 thoughts on “Convert unix timestamp to readable date

  1. python :
    datetime.datetime.fromtimestamp(timestamp).strftime(“%Y-%m-%d %H:%M:%S”)

    BSD :
    $ date -r 1335667666
    Sun Apr 29 10:47:46 CST 2012

Post Comment