GNU bug report logs - #63784
date --date "1 month ago" +%Y-%m does not work as expected on day 31

Previous Next

Package: coreutils;

Reported by: Jelle de Jong <jelledejong <at> powercraft.nl>

Date: Mon, 29 May 2023 13:41:01 UTC

Severity: normal

To reply to this bug, email your comments to 63784 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-coreutils <at> gnu.org:
bug#63784; Package coreutils. (Mon, 29 May 2023 13:41:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Jelle de Jong <jelledejong <at> powercraft.nl>:
New bug report received and forwarded. Copy sent to bug-coreutils <at> gnu.org. (Mon, 29 May 2023 13:41:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Jelle de Jong <jelledejong <at> powercraft.nl>
To: bug-coreutils <at> gnu.org
Subject: date --date "1 month ago" +%Y-%m does not work as expected on day 31
Date: Mon, 29 May 2023 13:55:04 +0200
Hello everybody,

I been hitting an issue for a while now that  date commands return the 
wrong month on day 31 of a month and my automations stops working on 
correctly on these days.

root <at> sydney:~# date
Wed Aug 31 22:09:04 CEST 2022
root <at> sydney:~# date --date "1 month ago" +%Y-%m
2022-07
root <at> sydney:~# date --date "2 month ago" +%Y-%m
2022-07
root <at> sydney:~# date --date "3 month ago" +%Y-%m
2022-05
root <at> sydney:~# date --date "4 month ago" +%Y-%m
2022-05

root <at> sydney:~# date
Sat Dec 31 19:20:13 CET 2022
root <at> sydney:~# date --date '1 month ago' +%Y-%m
2022-12

$ date --version
date (GNU coreutils) 8.30
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later

I would love to have this fixed if possible.

If I am using the wrong command I would love to be educated as well. 
Should I use date --date "$(date +%Y-%m-01) -1 month" +%Y-%m instead...

Kind regards,

Jelle de Jong




Information forwarded to bug-coreutils <at> gnu.org:
bug#63784; Package coreutils. (Mon, 29 May 2023 14:28:02 GMT) Full text and rfc822 format available.

Message #8 received at 63784 <at> debbugs.gnu.org (full text, mbox):

From: Pádraig Brady <P <at> draigBrady.com>
To: Jelle de Jong <jelledejong <at> powercraft.nl>, 63784 <at> debbugs.gnu.org
Subject: Re: bug#63784: date --date "1 month ago" +%Y-%m does not work as
 expected on day 31
Date: Mon, 29 May 2023 15:27:13 +0100
On 29/05/2023 12:55, Jelle de Jong wrote:
> Hello everybody,
> 
> I been hitting an issue for a while now that  date commands return the
> wrong month on day 31 of a month and my automations stops working on
> correctly on these days.
> 
> root <at> sydney:~# date
> Wed Aug 31 22:09:04 CEST 2022
> root <at> sydney:~# date --date "1 month ago" +%Y-%m
> 2022-07
> root <at> sydney:~# date --date "2 month ago" +%Y-%m
> 2022-07
> root <at> sydney:~# date --date "3 month ago" +%Y-%m
> 2022-05
> root <at> sydney:~# date --date "4 month ago" +%Y-%m
> 2022-05
> 
> root <at> sydney:~# date
> Sat Dec 31 19:20:13 CET 2022
> root <at> sydney:~# date --date '1 month ago' +%Y-%m
> 2022-12
> 
> $ date --version
> date (GNU coreutils) 8.30
> Copyright (C) 2018 Free Software Foundation, Inc.
> License GPLv3+: GNU GPL version 3 or later
> 
> I would love to have this fixed if possible.
> 
> If I am using the wrong command I would love to be educated as well.
> Should I use date --date "$(date +%Y-%m-01) -1 month" +%Y-%m instead...

Yes that is confusing.
The current workaround is to operate relative to the middle of the month as described at:
https://www.gnu.org/software/coreutils/faq/coreutils-faq.html#The-date-command-is-not-working-right_002e





This bug report was last modified 1 year and 183 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.