How do I convert an epoch timestamp to a human readable format on the cli?

393,010

Solution 1

On *BSD:

date -r 1234567890

On Linux (specifically, with GNU coreutils ≥5.3):

date -d @1234567890

With older versions of GNU date, you can calculate the relative difference to the UTC epoch:

date -d '1970-01-01 UTC + 1234567890 seconds'

If you need portability, you're out of luck. The only time you can format with a POSIX shell command (without doing the calculation yourself) line is the current time. In practice, Perl is often available:

perl -le 'print scalar localtime $ARGV[0]' 1234567890

Solution 2

date -d @1190000000 Replace 1190000000 with your epoch

Solution 3

If your epoch time is in milliseconds instead of seconds, remove the last three digits before passing it to date -d:

$ date -d @1455086371603
Tue Nov  7 02:46:43 PST 48079     #Incorrect

This gives incorrect data. Remove the last three digits.

$ date -d @1455086371
Tue Feb  9 22:39:31 PST 2016      #Correct after removing the last three digits. You may remove and round off the last digit too.

Solution 4

Custom format with GNU date:

date -d @1234567890 +'%Y-%m-%d %H:%M:%S'

Or with GNU awk:

awk 'BEGIN { print strftime("%Y-%m-%d %H:%M:%S", 1234567890); }'

Linked SO question: https://stackoverflow.com/questions/3249827/convert-from-unixtime-at-command-line

Solution 5

With bash-4.2 or above:

printf '%(%F %T)T\n' 1234567890

(where %F %T is the strftime()-type format)

That syntax is inspired from ksh93.

In ksh93 however, the argument is taken as a date expression where various and hardly documented formats are supported.

For a Unix epoch time, the syntax in ksh93 is:

printf '%(%F %T)T\n' '#1234567890'

ksh93 however seems to use its own algorithm for the timezone and can get it wrong. For instance, in Britain, it was summer time all year in 1970, but:

$ TZ=Europe/London bash -c 'printf "%(%c)T\n" 0'
Thu 01 Jan 1970 01:00:00 BST
$ TZ=Europe/London ksh93 -c 'printf "%(%c)T\n" "#0"'
Thu Jan  1 00:00:00 1970
Share:
393,010

Related videos on Youtube

xenoterracide
Author by

xenoterracide

Former Linux System Administrator, now full time Java Software Engineer.

Updated on September 17, 2022

Comments

  • xenoterracide
    xenoterracide almost 2 years

    How do I convert an epoch timestamp to a human readable format on the cli? I think there's a way to do it with date but the syntax eludes me (other ways welcome).

  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 13 years
    Assuming GNU date, that is.
  • Richard Hansen
    Richard Hansen over 12 years
    +1 for the comment about the lack of portability (why doesn't the POSIX spec include a way to do this? grr)
  • Chris Markle
    Chris Markle over 11 years
    What does the @ mean in date -d @1234567890? man date made no reference to that...
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 11 years
    @ChrisMarkle GNU man pages are often woefully incomplete. “The date string format is more complex than is easily documented here but is fully described in the info documentation.” To wit: gnu.org/software/coreutils/manual/html_node/…
  • Admin
    Admin almost 10 years
    Only works for GNU date and GNU awk. Neither awk nor nawk support strftime.
  • strangeronyourtrain
    strangeronyourtrain over 8 years
    The info date is quite complete. The entry at 28.9 Seconds since the Epoch explains in detail about the @timestamp.
  • Admin
    Admin over 8 years
    What utility prints included milliseconds (without a dot) ?
  • KnockTurnAl
    KnockTurnAl over 8 years
    I have seen that WebLogic Application server mostly returns data time values with milliseconds and no dots when using scripting tool. e.g., lastSuccessfulConnectionUse=1455086371603
  • Ginesu_Kun
    Ginesu_Kun almost 8 years
    In Linux in a pipe: date +'%s' | xargs -I n date -d @n
  • Br.Bill
    Br.Bill over 5 years
    Atlassian tools log their timestamps as epoch time with milliseconds.
  • user2320464
    user2320464 almost 5 years
    This technique is using Microsoft .NET. It doesn't seem OP is looking for an MS solution.
  • Stephen Kitt
    Stephen Kitt over 4 years
    Reviewers: PowerShell is available on Linux ;-).
  • ma11hew28
    ma11hew28 about 4 years
    To display the date in UTC, add the -u option.
  • saulius2
    saulius2 almost 4 years
    This doesn't work on HP-UX.
  • saulius2
    saulius2 almost 4 years
    This doesn't work on HP-UX.
  • saulius2
    saulius2 almost 4 years
    Does node.js run on HP-UX? Seemingly no: github.com/playnodeconf/ama/issues/10#issuecomment-211250773
  • saulius2
    saulius2 almost 4 years
    @StephenKitt, but does PowerShell run on HP-UX? Seeming no: reddit.com/r/PowerShell/comments/8cx8dp/… . And this is unix.
  • saulius2
    saulius2 almost 4 years
    It doesn't look like dateutils is available on HP-UX.
  • saulius2
    saulius2 almost 4 years
    Doesn't work on HP-UX: ./a.out 1599099168 YEAR=1901 MON=03 DAY=00 HOUR=2130568304 MIN=
  • saulius2
    saulius2 almost 4 years
    This actually works on HP-UX. Thanks!
  • Stephen Kitt
    Stephen Kitt almost 4 years
    @saulius2 by that reasoning, most of the content of Unix.SE is invalid, because it’s possible to find a Unix system where it isn’t applicable. This is Unix & Linux Stack Exchange.
  • saulius2
    saulius2 almost 4 years
    @StephenKitt, maybe it's so, I made no research about quality of the questions. But just by looking at the right side of this page I see three of them which are asking about the specific OS: unix.stackexchange.com/questions/86507/… unix.stackexchange.com/questions/96189/… unix.stackexchange.com/questions/434844/…
  • ijoseph
    ijoseph over 3 years
    date -r 1234567890 works on macOS's fork of BSD FWIW
  • Arvind Kumar Avinash
    Arvind Kumar Avinash almost 3 years
    It outputs the date-time in the system timezone. What do I need to do in order to get the date-time always in UTC?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 3 years
    @ArvindKumarAvinash env TZ=UTC date …
  • phette23
    phette23 almost 3 years
    Love this tclsh suggestion, that seems much easier to remember than most of the incantations in this thread