37

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.

1
  • Add a '+'-prefixed argument which would denote the format.
    – mike.dld
    Feb 22, 2014 at 18:57

5 Answers 5

58

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
1
  • 12
    the year 5881 ? I like your optimism. I think more likely the quantity 123439819723 refers to milliseconds, not seconds so you want date -r 123439819 '+%m/%d/%Y:%H:%M:%S' , which gives 11/29/1973:08:50:19 .
    – Cheeso
    Nov 19, 2015 at 21:04
24

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.

4
  • 15
    On OSX, I had to specify the unix time as the last parameter: date -j -f %s 1374432952
    – lionello
    Jun 10, 2014 at 6:22
  • 1
    Same here on FreeBSD. date -j -f %s 1374432952 worked.
    – cherdt
    Oct 23, 2018 at 15:16
  • 2
    That works on MacOS but the epoch time needs to be supplied in seconds (as opposed to milliseconds - from the question)
    – Pierz
    May 11, 2021 at 13:09
  • On OSX (as of 12.5), date -r 1374432952 works as well.
    – ozmo
    Sep 21, 2022 at 2:30
6

from command Shell

[aks@APC ~]$ date -r 1474588800
Fri Sep 23 05:30:00 IST 2016

[aks@APC ~]$ date -ur 1474588800
Fri Sep 23 00:00:00 UTC 2016

[aks@APC ~]$ echo "1474588800" | xargs -I {} date -jr {} -u
Fri Sep 23 00:00:00 UTC 2016
4

Combined solution to run on Mac OS.

Shell code:

T=123439819723 
D=$(date -j -f %s  $(($T/1000)) '+%m/%d/%Y:%H:%M:%S').$(($T%1000))
echo "[$T] ==> [$D]"

Output:

[123439819723] ==> [11/29/1973:11:50:19.723]

Or one line:

> echo 123439819723 | { read T; D=$(date -j -f %s  $(($T/1000)) '+%m/%d/%Y:%H:%M:%S').$(($T%1000));  echo "[$T] ==> [$D]" }
[123439819723] ==> [11/29/1973:11:50:19.723]
4

In case of Linux (with GNU coreutils 5.3+) it can be achieved using less keystrokes:

date -d @1608185188
#Wed Dec 16 22:06:28 PST 2020

On Mac one may need to install coreutils (brew install coreutils)

10
  • 1
    Does not work on mac os as of January 2022.
    – james-see
    Jan 24, 2022 at 15:29
  • @jamescampbell do you have gnu coreutils 5.3+?
    – jangorecki
    Jan 27, 2022 at 21:13
  • ? Here is what happens when I run the above command: ❯ date -d @1608185188 usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
    – james-see
    Jan 28, 2022 at 14:16
  • 1
    No. Your answer is about Linux. The question asked from the OP is about Mac OS / Unix. There is a difference. Based on your answer being for the wrong OS it seems like you should delete your answer and please have a look again at the original question posted by the OP. My comment is valid and my follow up is valid. If you want to answer how to do date conversion on linux, first find someone asking that and post your answer there (or ask it yourself). Otherwise you are forcing a square peg into a round hole and failed and wasting peoples time reading your answer that does not work. I tried!
    – james-see
    Jan 30, 2022 at 3:07
  • 1
    if you would have spent maybe like 1 extra minute, you would have seen this post stackoverflow.com/a/9805125/1215344 that explains the differences in the Unix / Linux date packages and that yes indeed, coreutils will install gdate, which is not the same thing at all as what the native (shipped with Mac OS) date package is. So, you could update your answer to say, "if you want to add a package to get this to work on Mac OS, you can do brew install coreutils and have gdate with the same functionality as Linux date command." That seems acceptable.
    – james-see
    Feb 2, 2022 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.