-2

I tried to convert the date pattern in a mac with the below command but it throws an error

date -j -f %m-%d-%Y 02-04-21 "+%Y-%m-%d" date: nonexistent time

tink
  • 14,342
  • 4
  • 46
  • 50
Venu
  • 3
  • 1
  • 2
    Which implementation of `date` accepts `-j`? – Arkadiusz Drabczyk Feb 23 '21 at 18:44
  • @ArkadiuszDrabczyk The implementation that comes with macOS (and maybe other BSD unixen?). It's [quite a bit different from the GNU (/Linux) verson](https://stackoverflow.com/questions/9804966/date-command-does-not-follow-linux-specifications-mac-os-x-lion). From the man page: "`-j` Do not try to set the date. This allows you to use the `-f` flag in addition to the `+` option to convert one date format to another." – Gordon Davisson Feb 23 '21 at 21:13

1 Answers1

0

The %Y format specifier (in the input format) indicates a 4-digit year (i.e. "2021" instead of just "21"). Either specify a 4-digit year, or use %y for a 2-digit year:

$ date -j -f %m-%d-%Y 02-04-2021 "+%Y-%m-%d"
2021-02-04
$ date -j -f %m-%d-%y 02-04-21 "+%Y-%m-%d"
2021-02-04
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151