0

I'm trying to convert a date string into epoch time in bash on macOS. I understand that the Mac's version of date is subtly different from on Linux - and I can't work out where I'm going wrong.

Here's an example expression I'm trying:

echo "$(date -j -u -f "%m/%d/%Y %H:%M:%S" +%s "08/08/20 20:20:00")"

This returns:

date: nonexistent time

Any clues?!

Moisie
  • 21
  • 1

1 Answers1

2

Ok - just needed a break from it in order to get my head around it. :-) What I needed to use is this:

date -j -u -f "%m/%d/%y %H:%M:%S" +%s "08/08/20 20:20:00"

(That is, %y instead of %Y)

The %Y was expecting a 4-digit year, so giving it 08/08/20 was referring to a day in 20AD - which is before the beginning of epoch time - hence nonexistent time.

Moisie
  • 21
  • 1