dateFormat() produces a string with wrong time

Hi,

I think I found another bug with the date functions in the server side. I’m using Windows 10, Wappler 5.5.3 and NodeJS.

I noticed that if I have a variable containing a date and I format it, the time shown in different from the time in the original variable, as if the formatDate() function used a date that is not the one in the variable.

The problem is easily reproducible. In the code below, all the variables are defined as dates.
Date0 is the starting date. It could be any date.
Date1 adds 0 days to Date 0, which allows me to see the hours, minutes, and seconds stored in the variable. (This is also reproducible if Date0 is defined with the time too.)

Note that although Date1 shows correctly the time for the Date0 variable that I entered, Date0_formatted and Date1_formatted do not.

I know that I’m 4 hours behind UTC, so I also tried forcing the variable to UTC and to LocalTime but the formatDate() function still does not produce the appropriate results. You can see that too in example provided below.

This is the code I used to try:
image

And these are the results I got:

Please let me know if I’m doing something wrong or expecting the wrong behaviour.

Because in my program I’m working on year end dates, I obtain the wrong year as a result, which is rather problematic, so I’d really appreciate a fix to this and the other date-related but that I reported recently (Date Add server function is not working properly).

Many thanks in advance!

Alex

Date and time is always something difficult to handle with all the different timezones. When you give a date 2021-10-01 it doesn’t contain any information about the time part, so it depends on the datetime parser what it will take as time. The date object in JavaScript normally works with the local timezone, so does our formatters which uses the methods of the JavaScript date object. The formatDate formatter outputs the date in local timezone, so in your example output it seems that the date was parsed as utc.

The problem in JavaScript is that you only have a Date object with date and time and not a separate date and time type. Even more difficult is that local time have different timezones and can have winter and summer time where the offset differs.

We use the native JavaScript Date object for the parsing. I did some testing and it seems that NodeJS Date object differs from the browser Date object in how it parses the date. In NodeJS your date string is seen as an utc date while in the browser it is seen as a local date. Adding a time to the date like 2021-10-01 00:00:00 will parse it as local time in both and 2021-10-01T00:00:00Z will parse it as utc in both. Seems that NodeJS has implemented the Date object a little different and causing these issues.

As a solution I could detect date strings and then parse them separately using local timezone to fix the issue. I can also add an utc variant of the formatters to work with utc datetimes on the server. Not sure which in your case would be the best solution. Did you try the same on the client with the App Connect formatters, there the date is being parsed using local timezone and would result in different results.

Hi Patrick,
Sorry for my late reply. A couple of thoughts:

  1. I cannot rely on the AppConnect formatters because the logic needs to be encapsulated in the back end.

  2. I am not experienced enough to answer your question, but I’m wondering if a more generic solution would be to add a parameter to the Wappler dateFormat() function that specifies which timezone (utc or local) it should use when interpreting the date on which it is acting. Not sure if this even makes sense, though…

Many thanks,

Alex

Following files are updated, they should fix the date parsing in NodeJS and I’ve added an extra argument to the formatDate formatter to use UTC date instead of local date.

util.zip (1.7 KB) unzip to lib/core
date.zip (823 Bytes) unzip to lib/formatters

The fixed date parsing probably fixes your problem, if you want to see what the utc formatDate outputs you have to edit the expression by hand and add true as second argument like Date1.formatDate('yyyy-MM-dd HH:mm:ss', true).