Skip to content
Advertisement

Date-fns Unexpected results when adding months

I am using date-fns for the first time in my project. I need to give user the ability to add a month to current date. I’m wondering how is this supposed to work with different length months?

I tried different months with 30 and 31 days and I cannot make sense of it. If I add a month to July 1, 2022 I get July 31, 2022 (I would expect Aug 1st) but if I add a month to July 7 I get Aug, 7, 2022.

Also, I added a month to Feb, 15 2023 and the result I got is Mar, 14 2023 (was expecting March 15). Then when I add a month to Feb 1, 2023 I get Mar 1, 2023 which is good.

Here is my code:

JavaScript

Appreciate if someone can advise. Thanks

Advertisement

Answer

as you havn’t given any examples of what you have done its impossible to tell you where you have gone wrong a quick example using the date you say don’t work

JavaScript

shows

JavaScript

so all working as expected

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement