Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 593
Nehal Shah Avatar asked Feb 22 '14 18:02

Nehal Shah


People also ask

How do you convert epoch time to human readable?

Convert from epoch to human-readable dateString date = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date (epoch*1000)); Epoch in seconds, remove '*1000' for milliseconds. myString := DateTimeToStr(UnixToDateTime(Epoch)); Where Epoch is a signed integer. Replace 1526357743 with epoch.

How do I convert epoch time to manual date?

You can take an epoch time divided by 86400 (seconds in a day) floored and add 719163 (the days up to the year 1970) to pass to it. Awesome, this is as manual as it gets.


2 Answers

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 
like image 112
mike.dld Avatar answered Sep 23 '22 01:09

mike.dld


To convert a UNIX epoch time with OS X date, use

date -j -f %s 123439819723 

The -j prevents date from trying to set the system clock, and -f specifies the input format. You can add +<whatever> to set the output format, as with GNU date.

like image 42
chepner Avatar answered Sep 26 '22 01:09

chepner