-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When on the last day of month, addMonth and subMonths doesn't return the expected date #3506
Comments
This is tricky because if you want subMonths of September 30 to be August 31, then should subMonths of September 29 be August 30? How about subMonths of September 29, 28, ... 1? Then is addMonths of August 31 becoming September 30 wrong? October 1 wouldn't work either. But you are right, having subMonth(addMonth(x)) != x feels very strange. And it feels strange for addMonths of August 30 to be the same as addMonths of August 31. |
@llllvvuu yep that's true, I had a second thought of this. I'm just curious if we should handle last-of-month subtraction with special consideration. But seems like it's not necessarily the case... |
@scopsy, be wary when creating dates like this: new Date("2023-08-31").toUTCString()
//=> 'Thu, 31 Aug 2023 00:00:00 GMT' But printing them with // in time zone ahead of UTC, e.g. 'Europe/Amsterdam'
new Date("2023-08-31")
// console.log will print:
Thu Aug 31 2023 02:00:00 GMT+0200 (Central European Summer Time)
// in time zone behind UTC, e.g. 'America/New_York'
new Date("2023-08-31")
// console.log will print:
Wed Aug 30 2023 20:00:00 GMT-0400 (Eastern Daylight Time)
d = new Date(2023, 7, 31).toUTCString()
'Thu, 31 Aug 2023 04:00:00 GMT'
console.log(d)
Thu, 31 Aug 2023 04:00:00 GMT |
@lemming thank you for the clarification! Yes, that was just used for the example here. Happens also when specified in the suggested format. Updated the original format |
I think Something like this: DescriptionAdd the specified number of calendar months to the given date. Warning! Due to the nature of calendar math this operation is lossy. E.g. Please see the examples below. Examples// Note that due to differences in the lengths of calendar months this operation is irreversible
// Add 1 month to 28 January 2023:
addMonths(new Date(2023, 0, 28), 1)
//=> Tue Feb 28 2023 00:00:00
// Add 1 month to 29 January 2023:
addMonths(new Date(2023, 0, 29), 1)
//=> Tue Feb 28 2023 00:00:00
// Add 1 month to 30 January 2023:
addMonths(new Date(2023, 0, 30), 1)
//=> Tue Feb 28 2023 00:00:00
// Add 1 month to 31 January 2023:
addMonths(new Date(2023, 0, 31), 1)
//=> Tue Feb 28 2023 00:00:00 // For the same reason adding months to the same date in conjunction and
// successively may lead to different results
// Add 1 month to October 31 2023
addMonths(new Date(2023, 9, 31), 1)
//=> Thu Nov 30 2023 00:00:00
// Add 2 month to October 31 2023
addMonths(new Date(2023, 9, 31), 2)
//=> Sun Dec 31 2023 00:00:00
addMonths(addMonths(new Date(2023, 9, 31), 1), 1)
//=> Sat Dec 30 2023 00:00:00 |
Sorry for nitpicking, but for anyone to easily reproduce the issue I would suggest one minor update. When using Date constructor from string months are indexed from 1, and when using individual date values, months are indexed from 0: new Date(2023, 7, 31) // August 31 2023
new Date(2023, 8, 31) // October 01 2023 |
Agreed regarding the documentation update point. It was our case and being a time based bug that only happens 3-4 times per year made harder to notice it. |
Reproduction
The expected results would be the 31st of August, while the printed result is the 30th of August.
I'm assuming that the subtraction should be in calendar months, hence subtracting should result in the last day of August.
Also, when doing this in February, this works as expected:
The text was updated successfully, but these errors were encountered: