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'
19 - is Date of a month 07 - is Month i.e. July in this case. 2021 - is Year
Desired output as ->
I am working on RH Linux with Date version as: date (GNU coreutils) 8.22
Please help to provide a solution for above problem.