How to convert timestamps to dates in Bash?
Solution 1:
On later versions of common Linux distributions you can use:
date -d @1267619929
Solution 2:
date -r <number>
works for me on Mac OS X.
Solution 3:
This version is similar to chiborg's answer, but it eliminates the need for the external tty
and cat
. It uses date
, but could just as easily use gawk
. You can change the shebang and replace the double square brackets with single ones and this will also run in sh
.
#!/bin/bash
LANG=C
if [[ -z "$1" ]]
then
if [[ -p /dev/stdin ]] # input from a pipe
then
read -r p
else
echo "No timestamp given." >&2
exit
fi
else
p=$1
fi
date -d "@$p" +%c
Solution 4:
You can use GNU date, for example,
$ sec=1267619929
$ date -d "UTC 1970-01-01 $sec secs"
or
$ date -ud @1267619929