Convert Unix timestamp to a date string
With GNU's date
you can do:
date -d "@$TIMESTAMP"
# date -d @0Wed Dec 31 19:00:00 EST 1969
(From: BASH: Convert Unix Timestamp to a Date)
On OS X, use date -r
.
date -r "$TIMESTAMP"
Alternatively, use strftime()
. It's not available directly from the shell, but you can access it via gawk. The %c
specifier displays the timestamp in a locale-dependent manner.
echo "$TIMESTAMP" | gawk '{print strftime("%c", $0)}'
# echo 0 | gawk '{print strftime("%c", $0)}'Wed 31 Dec 1969 07:00:00 PM EST
date -d @1278999698 +'%Y-%m-%d %H:%M:%S'
Where the number behind @
is the number in seconds
This solution works with versions of date which do not support date -d @
. It does not require AWK or other commands. A Unix timestamp is the number of seconds since Jan 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, then use:
date -r 1357004952
Command for getting epoch:
date +%s1357004952
Credit goes to Anton: BASH: Convert Unix Timestamp to a Date