macOS Unix time to Date String

Posted by

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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.