Skip to content

Instantly share code, notes, and snippets.

@sinewalker
Last active January 27, 2023 04:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinewalker/a5be89c3e70cfd6fbdcdfe0ba97a744c to your computer and use it in GitHub Desktop.
Save sinewalker/a5be89c3e70cfd6fbdcdfe0ba97a744c to your computer and use it in GitHub Desktop.
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.
[mjl@milo:~]
[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]

[mjl@milo:~]
[08:13] 1 $
@taha466
Copy link

taha466 commented Jan 27, 2023

Villan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment