36

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.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Nehal Shah
  • 363
  • 1
  • 3
  • 4

5 Answers5

55

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
Peter Munnings
  • 3,309
  • 1
  • 30
  • 43
mike.dld
  • 2,929
  • 20
  • 21
  • 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 '15 at 21:04
23

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.

danorton
  • 11,804
  • 7
  • 44
  • 52
chepner
  • 497,756
  • 71
  • 530
  • 681
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
ankursingh1000
  • 1,349
  • 1
  • 15
  • 21
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]
0x8BADF00D
  • 7,138
  • 2
  • 41
  • 34
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)

jangorecki
  • 16,384
  • 4
  • 79
  • 160
  • 1
    Does not work on mac os as of January 2022. – james-see Jan 24 '22 at 15:29
  • @jamescampbell do you have gnu coreutils 5.3+? – jangorecki Jan 27 '22 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 '22 at 14:16
  • It spits out the help file like I did something wrong. So I repeat, this does not work on Mac OS as of January 2022. It looks like a few of the answers below are more inclusive and include the usage for FreeBSD / Mac OS / Unix. – james-see Jan 28 '22 at 14:16
  • @jamescampbell please have a look again at question from my last comment above – jangorecki Jan 28 '22 at 22:35
  • 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 '22 at 03:07
  • I tried to be nice and say that it does not work on Mac OS. I should have just said this answer is not relevant to what the question is and flagged it as such. – james-see Jan 30 '22 at 03:11
  • Before deleting my answer I would prefer to be sure that installing gnu coreutils 5.3+ will still not work on Mac. – jangorecki Jan 31 '22 at 19:30
  • 1
    if you would have spent maybe like 1 extra minute, you would have seen this post https://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 02 '22 at 18:15
  • This still works, as of 2022-07. I probably have coreutils installed, though. – automorphic Jul 18 '22 at 23:24