macOS Unix time to Date String

I was working with an API today and received an authentication key, which prompted me to want to know how long it was good for. The returned expiration time was in Unix time1.

I knew I could convert the Unix time format into a readable format using the date command from the macOS terminal.

The following reads a Unix time format and converts to a string format.

➜  ~ date -r 1545966996                           
Thu Dec 27 22:16:36 EST 2018

The following reads a Unix time format and converts to a desired string format.

➜  ~ date -r 1545966996 '+%Y-%m-%d %T'
2018-12-27 22:16:36

The following will provide the current date & time in Unix time format.

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

Leave a Reply