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.
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.
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.
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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With