Convert Unix timestamp to date string

Is there a quick one-line way to convert a Unix timestamp to a date from a Unix command line?

date can work, except that it is rather inconvenient to specify each element (month, day, year, hour, etc.), and I cannot figure out how to make it work properly. It seems like it could be easier - am I missing something?

+104
unix bash unix-timestamp shell
Jul 14 '10 at 19:41
source share
11 answers

With date GNU you can do:

 date -d "@$TIMESTAMP" 
 # date -d @0 Wed Dec 31 19:00:00 EST 1969 

(From: BASH: convert Unix timestamp to date )

On OS X, use date -r .

 date -r "$TIMESTAMP" 

Or use strftime() . It is not accessible directly from the shell, but you can access it through gawk . The %c specifier displays the timestamp depending on the locale.

 echo "$TIMESTAMP" | gawk '{print strftime("%c", $0)}' 
 # echo 0 | gawk '{print strftime("%c", $0)}' Wed 31 Dec 1969 07:00:00 PM EST 
+146
Jul 14 '10 at 19:54
source share

date -d @1278999698 +'%Y-%m-%d %H:%M:%S' Where the number for @ is the number in seconds

+57
Jul 14 '10 at 20:01
source share

This solution works with date versions that do not support date -d @ . It does not require AWK or other commands. The Unix timestamp is the number of seconds since January 1, 1970, UTC, so it is important to specify UTC.

 date -d '1970-01-01 1357004952 sec UTC' Mon Dec 31 17:49:12 PST 2012 

If you are on a Mac, use:

 date -r 1357004952 

Team for getting an era:

 date +%s 1357004952 

Credit is sent to Anton: BASH: convert Unix timestamp to date

+21
Jan 01 '13 at 1:53 on
source share

As @TomMcKenzie notes in a comment on another answer, date -r 123456789 may be a more common (i.e. more widely implemented) simple solution within seconds of Unix Epoch, but unfortunately there is no universal guaranteed portable solution.

The -d option for many types of systems means something completely different than the GNU Date --date . Unfortunately, GNU Date does not interpret -r in the same way as these other implementations. Unfortunately, you need to know which version of date you are using, and many older date Unix commands do not support any of the options.

Worse, POSIX date does not recognize either -d or -r and does not provide a standard way in any command at all (that I know) to format Unix time from the command line (since POSIX Awk also lacks strftime() ). (You cannot use touch -t and ls because the former does not accept the time specified in seconds since Unix.)

Note that although One True Awk, available directly from Brian Kernighan, now has a built-in strftime() function, as well as a systime() function to return the current time in seconds since Unix Epoch), so maybe the Awk solution is most portable.

+9
Jan 01 '13 at 3:24
source share

If you find that the notation is inconvenient, perhaps the help is -R -option. It gives the date in RFC 2822 format. Thus, you do not need all of these identifiers: date -d @1278999698 -R . Another option is to display the date in seconds in your area: date -d @1278999698 +%c . It should be easy to remember. :-)

+4
Jul 14 2018-10-14T00:
source share

Standard Perl Solution:

 echo $TIMESTAMP | perl -nE 'say scalar gmtime $_' 

(or local time if necessary)

+2
Nov 06 '12 at 18:50
source share

A slight correction for dabest1's answer above. Specify the time zone as UTC, not GMT:

 $ date -d '1970-01-01 1416275583 sec GMT' Tue Nov 18 00:53:03 GMT 2014 $ date -d '1970-01-01 1416275583 sec UTC' Tue Nov 18 01:53:03 GMT 2014 

The second is correct. I think the reason is that in the UK, daylight saving time was constantly operating from 1968 to 1971.

+2
Nov 18 '14 at 10:45
source share

Other examples are hard to remember. In the simplest case:

 date -r 1305712800 
+2
Jun 30 '15 at 22:02
source share
 awk 'BEGIN { print strftime("%c", 1271603087); }' 
+1
Jul 14 '10 at 19:51
source share

Put the following in your ~ / .bashrc:

 function unixts() { date -d "@$1"; } 

Usage example:

 $ unixts 1551276383 Wed Feb 27 14:06:23 GMT 2019 
0
Feb 27 '19 at 14:18
source share

I work a lot with fixed periods of time, so I made my own tool at https://unixtime.ninja

0
Apr 16 '19 at 9:22
source share



All Articles