Scenario:
I am fetching a date value from a file into a variable and it is in DD-MM-YYYY format by default. I have to substract this date from system date. Subtraction is giving incorrect result if I had both dates in DD-MM-YYYY format. So I read a bit on google and decided to format both dates as YYYY-MM-DD as this will give correct value after subtraction. I have System date formatted successfully in YYYY-MM-DD, but facing a hard time to convert the date obtained from the file to YYYY-MM-DD format.
Below solution works fine with single digit dates:
$ date -d $(sed "s/-/\//g" <<< '9-2-1832') +%Y-%m-%d
Output : 1832-09-02
but when I try to convert date in double digits like below:
$ date -d $(sed "s/-/\//g" <<< '19-07-2021') +%Y-%m-%d
I get output
Invalid date '19/07/2021'
Where:
19 - is Date of a month
07 - is Month i.e. July in this case.
2021 - is Year
Desired output as -> 2021-07-19
I am working on RH Linux with Date version as: date (GNU coreutils) 8.22
Please help to provide a solution for above problem.
date
interpreted the date as if it was in US format (m-d-y) instead of d-m-y).LC_ALL=el_GR.utf8
orLC_ALL=es_ES.utf8
orLC_ALL=fr_FR.utf8
doesn't make a difference, although all of those use DD-MM-YYYY and print the right format with+%x
. This is a bug in GNUdate
, right?info date cal
date
weird: it jumps through hoops to try to accept all sorts of "intuitive" date inputs, but doesn't let you tell it what the format is when it's impossible to guess. (BSDdate
is much nicer in this respect.) Granged,19
can't be a month, but GNUdate
doesn't seem to go so far as to assume it must be a day and adjust accordingly.