How can I get a formatted date for a UNIX timestamp from the command line

  • I have a UNIX timestamp and I'd like to get a formatted date (like the output of date) corresponding to that timestamp.

    My attempts so far:

    $ date +%s
    $ date -d 1282367908
    date: invalid date `1282367908'
    $ date -d +1282367908
    date: invalid date `+1282367908'
    $ date +%s -d +1282367908
    date: invalid date `+1282367908'

    I'd like to be able to get output like:

    $ TZ=UTC somecommand 1282368345
    Sat Aug 21 05:25:45 UTC 2010
  • Correct answer

    10 years ago

    On Mac OS X and BSD:

    $ date -r 1282368345
    Sat Aug 21 07:25:45 CEST 2010
    $ date -r 1282368345 +%Y-%m-%d

    with GNU core tools (you have to dig through the info file for that):

    $ date -d @1282368345
    Sat Aug 21 07:25:45 CEST 2010
    $ date -d @1282368345 --rfc-3339=date

    With either, add the -u (standard) option, or pass a TZ=UTC0 environment variable to have the UTC date (TZ=UTC0 defines a timezone called UTC with offset 0 from UTC while the behaviour for TZ=UTC (with no offset) is unspecified (though on most systems would refer to a system-defined timezone also called UTC with offset 0 from UTC)).

    It takes a lot of digging, though, which is surprising for such a common task.

    Thanks, accepted your answer. I should have read `info coreutils date` a little more carefully.

    This is giving me a slightly different date: `date -r 1447264553943` results in `Wed Dec 28 01:39:03 BRST 47831`, when it's actually `11/11/2015, 3:55:53 PM GMT-2:00`.

    @falmp: it will work if you use seconds instead of microseconds.

    @hop ops, should have figured with the year being 47831, but that number was so high I missed it. Thanks!

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM