Homey Community Forum

Local time as string

Hi @all,
in an app I want to updatea string capability with the local time (YYYY-mm-DD HH:MM).
The new Date() instance returns the UTC time. So how can I convert it to the local time when new date().getTimezoneOffset() return 0 instead of the real timezone offset?
Is there another way to get the local time respecting timezone?

You can get something like DD/mm/YYYY, HH:MM:SS like this:

const tz  = this.homey.clock.getTimezone();
const now = new Date().toLocaleString('en-GB', { timeZone: tz });

If you want more control, you need to use an external library like moment or luxon, which is basically what Athom suggests when I posted about this issue.

I believe that the reason for Athom to break Date for all SDK3 apps is that when a user changes their Homey’s location/timezone, already-running apps will still be using the previous timezone, which may cause issues. I guess people changing Homey locations happens so often that it warranted causing a ball-ache for all developers using times/dates.


I’ve seen the the .getTimezone() but didn’t know that I can use this string value in toLocaleString(). So thanks for the exampme. I’ll test it later.

With timezone I get the local time. But the locales (en-GB, de-DE) are not working in the way I hoped.
en-US seams to be ok, but with en-GB and de-DE I get MM/DD/YYYY. It should be DD.MM.YYYY.
So I’m shifting the values into a new var in correc order.

Are you sure? I would expect the other way around (US showing month first, day second, and European showing day first, month second). At least that’s how it’s working for me.

I would expect it, too.

But with this example…

const tz  = this.homey.clock.getTimezone();
    var options =     { 
      hour12: false, 
      timeZone: tz,
      hour: "2-digit",
      minute: "2-digit",
      day: "2-digit",
      month: "2-digit",
      year: "numeric"
    console.log(new Date().toLocaleString('de-DE', options)); 
    console.log(new Date().toLocaleString('en-US', options)); 
    console.log(new Date().toLocaleString('en-GB', options));

I get this output:

Ah yes, you’re right. I tested with Node 14 but Homey still runs Node 12, which doesn’t seem to support localized formats.

Node 14:

1.5.2021, 12:34:56
5/1/2021, 12:34:56
01/05/2021, 12:34:56

Node 12:

5/1/2021, 12:34:56
5/1/2021, 12:34:56
5/1/2021, 12:34:56