Date/timzone conversion issue with .toLocaleString()

I just want to share my experiences with timezone conversion using .toLocaleString( timeZone: tz,…).
This often used to get the curernt local time for logging etc.

This method has a bug at least und HomeyPro19 NodeJS version. I haven’t tested it on HP23 yet.
The error occours if a time is used, when UTC is one day back compared to your local time.

Example:
Time is: 07/03/2023, 22:00 and timezone difference is 2h
Local time sould be: 07/04/2023, 00:00

Then convert into local time string using:

        let tz  = this.homey.clock.getTimezone();
        let now = new Date().toLocaleString('en-US', 
        { 
            hour12: false, 
            timeZone: tz,
            hour: "2-digit",
            minute: "2-digit",
            day: "2-digit",
            month: "2-digit",
            year: "numeric"
        });

Result: 07/04/2023, 24:00 (means: it’s 07/05/2023 0:00)
When converting this string back into a date, you get: 07/05/2023 0:00 (one day ahead)

Example 2:
Time is: 07/03/2023, 22:01 and timezone difference is 2h
Converted with .toLocaleString() you get: 07/04/2023, 24:01 (means: 07/05/2023 0:01)

So better use another date library to convert timezones :wink:

It’s not strictly a bug, using “24:00” is actually valid in a 24-hour clock.

According to this, it’s also within spec, however, the spec was wrong (specifically when using 12-hour locales in “24 hour mode”, like you’re doing).

The spec has since been fixed, but at least the current version of Node.js (v20) will still happily show 24:00 (and 24:01, …) so I assume both HP2019 and HP2023 will be affected.

The main issue here is using a function that is meant for display purposes (toLocaleString()) for purposes other than intended, and also using a locale that’s not the most appropriate for what you’re using it for (en-US).

Dates (and times) are hard to get right, and running apps with TZ=UTC makes it even more difficult, so I would really suggest anyone that has to deal with them in their app to use a library like Moment or Luxon to handle all the intricacies.

1 Like