Home > Mobile >  Is "01/02/2022 1 month" saying output is 2/2/2022 a bug in the date command?
Is "01/02/2022 1 month" saying output is 2/2/2022 a bug in the date command?

Time:02-06

on bash (GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)), I am adding a month to a certain date using the following operation (please note the format):

date  %d/%m/%Y -d "01/02/2022   1 month"

It gives 02/02/2022 (wrong it is adding the 1 month to the day). Then I tried:

date  %d/%m/%Y -d "01/02/2022   2 month"

It gives 02/03/2022 (half wrong, it is adding 1 to the day and 1 to the month). Instead:

date  %d/%m/%Y -d "02/02/2022   1 month"

gives: 02/03/2022 (correct).

Did I find a bug?

CodePudding user response:

No. You used a date format that can be interpreted in two ways and date (not related to bash) chose the interpretation different to yours:

  • January 2nd 1 month = February 2nd, OK.
  • January 2nd 2 months = March 2nd, OK.

The third date is the same in both the interpretations.

Note that the input format is independent of the output format.

Conclusion: Always use %Y-%m-%d. It can be sorted alphabetically and is not interpreted as %Y-%d-%m as no one uses that format.

  •  Tags:  
  • Related