8/3/2017 - 10:15 PM

Unix Epoch conversion

Unix Epoch conversion

Convert a Unix timestamp into human-readable format:

  • GNU: date -date='@1501768813'
  • BSD: date -r 1501768813

Convert the other way

  • Current date/time as Unix timestamp: date +%s

  • A specific date/time (assumes current timezone):

    • GNU: date -d '2017-08-13 07:18:42' +%s
    • BSD: date -j -f '%Y-%m-%d %H:%M:%S' '2017-08-13 07:18:42' +%s
  • A specific date/time with timezone (not sure if these are correct -- UTC+11 in this example):

    • GNU: date -d '2017-08-13 07:18:42+11' +%s
    • BSD: date -j -f '%Y-%m-%d %H:%M:%S' -v +11H '2017-08-13 07:18:42' +%s

Note that the BSD manual for date(1) has an error, at least on MacOS:

Finally the command:

      date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"

can be used to parse the output from date and express it in Epoch time.
[08:12]$ date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"
Failed conversion of ``Fri  4 Aug 2017 08:13:55 AEST'' using format ``%a %b %d %T %Z %Y''
date: illegal time format
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

[08:13] 1 $