Convert a Unix timestamp to UTC on the macOS command line.

If you have a Unix timestamp and you want to convert it to a human-readable format from the command line on macOS the date command can help.

$ date -ur 1629910494
Wed 25 Aug 2021 16:54:54 UTC

The shortest syntax is using the -r option followed by the Unix timestamp string.

A more verbose version of the same thing is to use the -f option and pass %s which indicates the timestamp is formatted as the number of seconds since the Unix Epoch.

$ date -ju -f "%s" 1629872092
Wed 25 Aug 2021 06:14:52 UTC

You’ll notice the above output is in UTC. If you would like to convert to your computer’s local time zone, omit the -u option.

$ date -r 1629872092
Wed 25 Aug 2021 14:14:52 WITA

Of course, you can combine this with other command-line techniques to convert Unix timestamps you coming via stdIn or sub-shells.

$ date -ju -f "%s" $(vipgo api GET '/sites/4228/data-sync/progress' | jq -r '.startTime')

Above is an example of using a subshell, while below we use xargs to pass output to and execute date.

$ vipgo api GET '/sites/4228/data-sync/progress' | jq -r '.startTime' | xargs date -ur
Wed 25 Aug 2021 16:54:54 UTC

Bonus: See the current time in UTC from the command line

$ date -u
Wed 25 Aug 2021 10:20:44 UTC

This quick command returns the current time in UTC format for your cross-time-zone scheduling needs.






Leave a Reply

Your email address will not be published. Required fields are marked *

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