How to I get the GNU date
command to interpret dates in the format 'dd/mm/yyyy', for example:
What I get:
$ date -d '09/07/2016'
Wed, Sep 7, 2016 12:00:00 AM
What I want:
$ date -d '09/07/2016'
Sat, Jul 9, 2016 12:00:00 AM
I've tried setting LC_ALL
but to no avail.
Best Answer
Note that
date -d
is a GNU extension. The syntax it expects is fixed and independent from the locale. In particular, it does not use POSIXgetdate()
to parse the date (even though the interface GNUdate
uses used to be calledgetdate()
as well).Some builds of some versions of busybox also recognise a
-d
option, but the range of supported formats is narrower. In particular, it does not recognisemm/dd/YYYY
. You can however pass a-D <strptime-format>
option (thanks @BinaryZebra) to specify the format you like:AT&T (ast-open)
date
, since 1995, also supports a GNU-style-d
option. That one does use POSIXgetdate()
. So with thatdate
implementation, you can use theDATEMSK
variable to change the way dates are parsed and since 1996, you can use the-p
option to pass astrptime
-style parsing format on the command line:AT&T
ksh93
'sprintf %T
also uses agetdate()
-compatible API and so can also be affected by theDATEMSK
variable, so in ksh93, you can do:To parse a date portably in a
zsh
script, you can used thestrftime
builtin (after loading it) with the-r
option:zsh
'sstrftime -r
uses POSIXstrptime()
to parse the string. As such, it is locale-dependant. In a French locale for instance, you can usestrftime -r %d-%b-%Y 14-juillet-2016
to parse a French date.Some
strptime()
implementations like GNU's still accept the English month/day names when in non-English locales, but not all (Solaris 11's one doesn't for instance). Something to bear in mind when parsing an English date in the user's locale.Recent versions of
bash
also have a ksh-styleprintf '%(format)T'
, but that one has no time parsing capability.For completeness, on BSDs,
date
accepts a-f <strptime-format>
which you can use in combination with-j
to reformat a date: