# Date/timzone conversion issue with .toLocaleString()

**URL:** <https://community.homey.app/t/date-timzone-conversion-issue-with-tolocalestring/83933>\
**Category:** Developers\
**Created:** [July 4, 2023, 5:59pm UTC](https://community.homey.app/t/date-timzone-conversion-issue-with-tolocalestring/83933 "2023-07-04T17:59:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![RonnyW](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/ronnyw/32/10879_2.png) [@RonnyW](https://community.homey.app/u/RonnyW)\
**Post date:** [July 4, 2023, 5:59pm UTC](https://community.homey.app/t/date-timzone-conversion-issue-with-tolocalestring/83933/1 "2023-07-04T17:59:11Z")

</div>

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:

```auto
        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 😉

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [July 4, 2023, 7:39pm UTC](https://community.homey.app/t/date-timzone-conversion-issue-with-tolocalestring/83933/2 "2023-07-04T19:39:57Z")

</div>

> [@RonnyW](#):
>
> This method has a bug at least und HomeyPro19 NodeJS version. I haven’t tested it on HP23 yet.

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

According [to this](https://github.com/moment/luxon/issues/726#issuecomment-675151145), 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.
