2

The weirdest thing happens to date calculation because of what you see below; notice that adding one month to Oct 31st takes it to December! This can't be? I have the same issue on Ubuntu 16 and 20. This issue also causes my shell script to give the wrong date, so it's not just the cron scheduler.

root@ubuntugc:/home/miuser# date -d "20210930+1 month" +%Y-%m-%d
2021-10-30
root@ubuntugc:/home/miuser# date -d "20211030+1 month" +%Y-%m-%d
2021-11-30
root@ubuntugc:/home/miuser# date -d "20211031+1 month" +%Y-%m-%d
2021-12-01

1 Answer 1

7

This is how date works, when adding 1 month it just increases month by one, in your case to 2021-11-31 which is a date that doesn't exist so it then moves the result one day forward to a valid date (2021-12-01). From coreutils documentation:

The fuzz in units can cause problems with relative items. For example, ‘2020-07-31 -1 month’ might evaluate to 2020-07-01, because 2020-06-31 is an invalid date. To determine the previous month more reliably, you can ask for the month before the 15th of the current month. For example:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.