# Local time in HomeyScript

**URL:** <https://community.homey.app/t/local-time-in-homeyscript/33338>\
**Category:** Questions & Help\
**Tags:** homeyscript\
**Created:** [August 7, 2020, 7:04am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338 "2020-08-07T07:04:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![iotnerd](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/iotnerd/32/27884_2.png) [@iotnerd](https://community.homey.app/u/iotnerd)\
**Post date:** [August 7, 2020, 7:04am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/1 "2020-08-07T07:04:53Z")

</div>

Is this really the best way to get the local time in HomeyScript or am I missing something?

```
const sys = await Homey.system.getInfo();
console.log('sys datetime', sys.date)
const datetime = new Date(sys.date)
datetime.setMinutes(datetime.getMinutes() - new Date().getTimezoneOffset())
console.log('local datetime', datetime)

sys datetime 2020-08-07T07:01:36.101Z
local datetime 2020-08-07T09:01:36.101Z
```

---

<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:** [August 7, 2020, 7:36am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/2 "2020-08-07T07:36:52Z")

</div>

Not really clear what exactly you want? If it’s the current local time in a string:

```auto
const time = new Date().toTimeString().split(' ')[0];

```

---

<div class="post-metadata">

**Author:** ![iotnerd](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/iotnerd/32/27884_2.png) [@iotnerd](https://community.homey.app/u/iotnerd)\
**Post date:** [August 7, 2020, 7:39am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/3 "2020-08-07T07:39:32Z")

</div>

I need to do calculations with local time to follow sun position at my location.

---

<div class="post-metadata">

**Author:** ![tlangelaar](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tlangelaar/32/10980_2.png) [@tlangelaar](https://community.homey.app/u/tlangelaar)\
**Post date:** [August 7, 2020, 7:42am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/4 "2020-08-07T07:42:51Z")

</div>

Might the [Sun Events app](https://homey.app/en-us/app/com.cyclone-software.sunevents/Sun-Events/) be of help?

---

<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:** [August 7, 2020, 7:43am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/5 "2020-08-07T07:43:17Z")

</div>

Most of the `.get*` methods return local values (so `new Date().getHours()` returns the hours according to local time)

---

<div class="post-metadata">

**Author:** ![iotnerd](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/iotnerd/32/27884_2.png) [@iotnerd](https://community.homey.app/u/iotnerd)\
**Post date:** [August 7, 2020, 7:49am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/6 "2020-08-07T07:49:46Z")

</div>

Good tip! I’ll use this app for certain automations. In this case I’m adjusting the color temperature of Hue bulbs throughout the day as if it was always April 16th (a nice long spring day) helping the circadian rhythm keep steady.

---

<div class="post-metadata">

**Author:** ![iotnerd](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/iotnerd/32/27884_2.png) [@iotnerd](https://community.homey.app/u/iotnerd)\
**Post date:** [August 7, 2020, 7:52am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/7 "2020-08-07T07:52:03Z")

</div>

Good point! Unfortunately, I’m doing calculations with milisecs.

---

<div class="post-metadata">

**Author:** ![iotnerd](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/iotnerd/32/27884_2.png) [@iotnerd](https://community.homey.app/u/iotnerd)\
**Post date:** [August 7, 2020, 7:58am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/8 "2020-08-07T07:58:54Z")

</div>

Here is my script for anyone interested: [https://gist.github.com/suhajdab/2f162833369751a07239b6736ddb6d5f](https://gist.github.com/suhajdab/2f162833369751a07239b6736ddb6d5f)

---

<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:** [August 7, 2020, 9:59am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/9 "2020-08-07T09:59:21Z")

</div>

This:

```auto
const sunrise = getLocalDatetime(sys.date).setHours(5, 59);

```

Is the same as this:

```auto
const sunrise = new Date(sys.date).setHours(5, 59);

```

And in fact, I don’t think this actually does what you think it does:

```auto
const now = getLocalDatetime(sys.date).getTime();

```

Because `setHours` already sets the _local_ time, you don’t have to use `getLocalDatetime()` to adjust for the timezone: the adjustment that you do now gets reverted by using `setHours`. However, that reversion doesn’t happen with `.getTime()`, which means that `now` will actually be “now + X hours” (where X is the current timezone offset).

To demonstrate the issue, you can add this to your code:

```auto
const sunrise = getLocalDatetime(sys.date).setHours(5, 59);
const sunset = getLocalDatetime(sys.date).setHours(20, 16)
const now = getLocalDatetime(sys.date).getTime();

const _ = d => new Date(d).toString()

console.log('sunrise', _(sunrise), _(new Date(sys.date).setHours(5, 59)));
console.log('sunset ', _(sunset), _(new Date(sys.date).setHours(20, 16)));
console.log('now ', _(now), _(Date.now()));

```

For me, this displays the following:

```auto
sunrise Fri Aug 07 2020 05:59:20 GMT+0200 (Central European Summer Time) Fri Aug 07 2020 05:59:20 GMT+0200 (Central European Summer Time)
sunset Fri Aug 07 2020 20:16:20 GMT+0200 (Central European Summer Time) Fri Aug 07 2020 20:16:20 GMT+0200 (Central European Summer Time)
now Fri Aug 07 2020 13:57:20 GMT+0200 (Central European Summer Time) Fri Aug 07 2020 11:57:20 GMT+0200 (Central European Summer Time)

```

As you can see, both `sunrise` and `sunset` show the exact same strings, regardless of whether `getLocalDatetime()` gets called or not.

You can also see that “your” value of `now` is showing a timestamp 2 hours into the future (it’s 11:57 here at the moment).

---

<div class="post-metadata">

**Author:** ![iotnerd](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/iotnerd/32/27884_2.png) [@iotnerd](https://community.homey.app/u/iotnerd)\
**Post date:** [August 7, 2020, 11:10am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/10 "2020-08-07T11:10:37Z")

</div>

Excellent point! Thanks for taking the time! 🙂  
Now it does correctly reflect the progress of the sun for April 16. 👍

---

<div class="post-metadata">

**Author:** ![iotnerd](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/iotnerd/32/27884_2.png) [@iotnerd](https://community.homey.app/u/iotnerd)\
**Post date:** [March 30, 2021, 7:22am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/11 "2021-03-30T07:22:58Z")

</div>

Hey! It seems my local time is broken again in v5. Do you have any ideas on how to fix my script again?

---

<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:** [March 30, 2021, 9:08am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/12 "2021-03-30T09:08:43Z")

</div>

With SDKv3 (part of v5) Athom has decided to break `Date` functionality so it _always_ returns times using the universal (UTC) timezone, even for methods that are supposed to return local times.

See [this issue on Github](https://github.com/athombv/homey-apps-sdk-issues/issues/169).

As of yet, Athom hasn’t given a satisfactory answer on how Homeyscript users should deal with this.

---

<div class="post-metadata">

**Author:** ![tb1962](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tb1962/32/7888_2.png) [@tb1962](https://community.homey.app/u/tb1962)\
**Post date:** [March 30, 2021, 10:54am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/13 "2021-03-30T10:54:57Z")

</div>

You can use this code:

`new Date().toLocaleString("nl-NL", {hour12: false, timeZone: "Europe/Amsterdam"})`

Use whatever timezone you are living in

---

<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:** [March 30, 2021, 11:01am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/14 "2021-03-30T11:01:35Z")

</div>

And then what? How can you perform calculations based on the local time with that?

---

<div class="post-metadata">

**Author:** ![tb1962](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tb1962/32/7888_2.png) [@tb1962](https://community.homey.app/u/tb1962)\
**Post date:** [March 30, 2021, 11:06am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/15 "2021-03-30T11:06:13Z")

</div>

```
if you want a date object you need a second line of code:

const now = new Date().toLocaleString("nl-NL", {hour12: false, timeZone: "Europe/Amsterdam"});
**log(new Date(now))**
```

---

<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:** [March 30, 2021, 11:37am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/16 "2021-03-30T11:37:24Z")

</div>

That’s basically just adding two hours to a UTC timestamp. Calculations will break for timestamps that will cross DST boundaries because UTC doesn’t observe DST, and I’m sure there are other edge cases too (handling dates and times is hard).

Another issue is that the output of `toLocaleString('nl-NL')` doesn’t yield a parseable date on Node v14 and up. That can also be worked around, but we’re piling up the workarounds here.

All in all not a satisfactory solution in my opinion.

---

<div class="post-metadata">

**Author:** ![tb1962](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/tb1962/32/7888_2.png) [@tb1962](https://community.homey.app/u/tb1962)\
**Post date:** [March 30, 2021, 11:38am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/17 "2021-03-30T11:38:55Z")

</div>

At this moment it’s all we have, i think  
Homey is currently using version 12.16

---

<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:** [March 30, 2021, 11:46am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/18 "2021-03-30T11:46:20Z")

</div>

Yes, that’s true, but it really bothers me that Athom has implemented this change without considering the implications.

The reason why they implemented this is, as far as I know, to work around possible issues when apps are running and the user changes their timezone. In that case, the running apps will still be running in the “old” timezone, and this may lead to confusion.

But their fix, running all apps in TZ=UTC, is quite honestly terrible, because it now breaks all apps that deal with times/dates and expect the standard (namely that `Date` objects will return local times/dates unless you explicitly use the UTC-based methods) to apply.

A user will change their Homey’s timezone once, perhaps twice, during the lifetime of the device. I don’t see any problems with either telling the user that changing the timezone may cause certain issues, or to just tell them that Homey needs to reboot after a timezone change. But they haven’t done so, because, I assume, “that’s not user friendly”. Instead, we have broken apps and scripts now, and have to think about workarounds to fix them.

And yes, Homey runs Node v12 _now_, but I expect them to update to v14 or even v15 in the not so distant future.

---

<div class="post-metadata">

**Author:** ![Fredrik\_Palsson](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/fredrik_palsson/32/34467_2.png) [@Fredrik\_Palsson](https://community.homey.app/u/Fredrik_Palsson)\
**Post date:** [June 10, 2021, 9:44pm UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/19 "2021-06-10T21:44:06Z")

</div>

Has there been a proper solution to this? I’m experiencing problems in my app because of this!

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [June 10, 2021, 10:55pm UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/20 "2021-06-10T22:55:10Z")

</div>

Extract it from “date human”.

[Next page](https://community.homey.app/t/local-time-in-homeyscript/33338.md?page=2)
