Date Add server function is not working properly

Hi,
I’m using Wappler 5.5.3, on Windows 10 and NodeJS

I believe that there is a bug with the server-side dateAdd() function when adding months.

In short, dateAdd(‘months’, n ) seems to produce the wrong results

Thus, if my starting date is say Jan 1, and I add months, the result produced are like this:

The problem is easily reproducible. The code below can be used to show the behaviour.
image

And this is the result:

It looks like the dateAdd (‘months’, n) function only returns the right value when the ending month has 30 days. In this example, it is OK only when n = 4 or n = 6.

I hope this helps and that this bug can be fixed soon.
Many thanks in advance,

Alex

PS: A few years ago, I reported a bug with the same function that looks related to this one, although at the time I was using the PHP server model. It’s here: Problem subtracting months on the server side - Bug Report from aschoijett #2021-1-2_13-8-9

Thanks for finding that out @aschoijett!

I guess a work-around would be to de-construct the 3 numbers from the 2010-01-01 format, add one to to middle number and then put it back together again!

The date formatters work with local timezones, for the output server connect uses utc time. The problem you see is with the change from winter to summer time in your timezone, the clock moved 1 hour back which is why you see 23:00 from march on. Giving Date0 a value 2010-01-01 00:00:00 and use the toLocalTime() formater on the other expressions will output the correct dates. The formatter isn’t really doing something wrong, we probably need more control over timezones or different formatters for local and utc.

Hi Patrick,
Thank you for your reply, but I think that there are two problems here. One that may be related the time zone logic and another to the dateAdd() logic when the added period is ‘month’. The latter is what I was trying to report in this post.

I run the the code below:

And obtained these results:

PROBLEM 1
Note the difference between NOW and NOW_UTC. As you can see, it is 4 hours, which is correct at this time of the year between UTC and my local time – Toronto (EDT) time.

What follows shows that a hardcoded date (startng at 00:00:00) is indeed interpreted as a utc time because coverting it to UTC yields the same result as the hardcoded value. However, converting it to Local Time shows a 5-hour difference, not the 4-hour difference between NOW and NOW_UTC.

PROBLEM 2
Part 3 takes a date that starts on Jan 1, 2010 at 00:00:00 and successively adds months to it using the dateAdd(‘month’, N) function. My expectation was that this function, when applied to the the first day of a month, would generate the first day of the Nth month added. So if I start on Jan 1 and add 8 months to it, I should be in Sep 1. As you can see, this is not the case, and although all the times are 00:00:00 (as expected, adding months should not change the date), the dates (mm-dd) are wrong in all the highlighted outputs.

Note that in some cases, the difference is a few days! For example, see what happens after adding 2 months to Jan 1: instead of producing March 1, it produces March 4.

This is why I cannot believe that this is caused by differences between utc and local times. In all likelihood, this a different problem from the one with the time zones shown above.

Hope this helps solve the problems. In my case, Problem 2 has a much higher priority.

Many thanks!

Alex

PS:
As said in the original post, I reported a somewhat related problem a few years ago, although that one was using the PHP server.

1 Like

Sorry for the late respone.

Actually both times are correct, the localtime in January is UTC-5h (winter time) while in July it is UTC-4h (summer time). Due to DST the offset changes, you change your clocks in March and November by 1 hour.

There are indeed some that are not correct, I don’t understand where March 4 comes from. The dates with 31 are due to DST, you can see that it has 23:00 (-1 hour). I think it has to do with the DST, it is in March and November when it changes and it is exactly there where it seems to go wrong. Will try to investigate this further.