Convert unix epoch time to human readable date on Mac OSX - BSD

Nehal Shah picture Nehal Shah · Feb 22, 2014 · Viewed 39.7k times · Source

On my Mac OSX, my bash script has a epoch time 123439819723. I am able to convert the date to human readable format by date -r 123439819723 which gives me Fri Aug 26 09:48:43 EST 5881.

But I want the date to be in mm/ddd/yyyy:hh:mi:ss format. The date --date option doesn't work on my machine.

Answer

mike.dld picture mike.dld · Feb 22, 2014

Here you go:

# date -r 123439819723 '+%m/%d/%Y:%H:%M:%S'
08/26/5881:17:48:43

In a bash script you could have something like this:

if [[ "$OSTYPE" == "linux-gnu"* ]]; then
  dayOfWeek=$(date --date @1599032939 +"%A")
  dateString=$(date --date @1599032939 +"%m/%d/%Y:%H:%M:%S")
elif [[ "$OSTYPE" == "darwin"* ]]; then
  dayOfWeek=$(date -r 1599032939 +%A)
  dateString=$(date -r 1599032939 +%m/%d/%Y:%H:%M:%S)
fi