# 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:** 18\
**Page:** 2

<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, 11:01pm UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/21 "2021-06-10T23:01:52Z")

</div>

> Extract it from “date human”.

Ehhh?

---

<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:** [June 11, 2021, 4:26am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/22 "2021-06-11T04:26:08Z")

</div>

I think your best bet is to use a library like `moment`, `luxon` or `dayjs`.

---

<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 11, 2021, 5:06am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/23 "2021-06-11T05:06:33Z")

</div>

> [@robertklep](#):
>
> I think your best bet is to use a library like `moment` , `luxon` or `dayjs` .

Thanks! Will do. Not impressed by that design decision from Athom…

---

<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:** [June 11, 2021, 6:15am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/24 "2021-06-11T06:15:47Z")

</div>

> [@Fredrik\_Palsson](#):
>
> Not impressed by that design decision from Athom…

Neither am I 🙄

---

<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 11, 2021, 9:21am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/25 "2021-06-11T09:21:00Z")

</div>

```auto
let SysInfo = await Homey.system.getInfo();
//theDay = SysInfo.dateHuman.slice(0,(SysInfo.dateHuman.indexOf(' ')));
  theHour = SysInfo.dateHuman.slice((SysInfo.dateHuman.length)-8,(SysInfo.dateHuman.length)-6);
//theMin = SysInfo.dateHuman.slice((SysInfo.dateHuman.length)-5,(SysInfo.dateHuman.length)-3);
//theHour = "08"; // test value
theHour = theHour - 0; // to prevent 08 , 09 a.s.o.

```

---

<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:** [June 11, 2021, 9:39am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/26 "2021-06-11T09:39:23Z")

</div>

Apart from having to (be allowed to\*) use the Web API permission for an app, this method (or any method that tries to parse some sort of string) is fraught with potential issues ☹

\*: a Slack user’s app was rejected by Athom because they were using the Web API in their app. Maybe that was just a mistake on Athom’s part, or maybe they are clamping down on apps using the Web API, I don’t know.

---

<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 11, 2021, 11:02am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/27 "2021-06-11T11:02:10Z")

</div>

This is a too high-level language for me. Was it a reaction to my post?

---

<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:** [June 11, 2021, 11:13am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/28 "2021-06-11T11:13:28Z")

</div>

Yes, it was 😃

---

<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 11, 2021, 1:17pm UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/29 "2021-06-11T13:17:10Z")

</div>

Tried this, but to no avail. What am I missing?

`let dateLocal= moment.utc(measurementTimestampUTC).local().toDate()`

---

<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:** [June 11, 2021, 1:53pm UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/30 "2021-06-11T13:53:20Z")

</div>

> [@Fredrik\_Palsson](#):
>
> What am I missing?

UTC is the local timezone as far as Homey is concerned.

Perhaps it’s easier to use `luxon`:

```auto
const { DateTime } = require('luxon');

const utc = new DateTime(new Date().toISOString());
console.log(utc.toString());
const local = utc.setZone(this.homey.clock.getTimezone());
console.log(local.toString());

```

---

<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 11, 2021, 2:05pm UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/31 "2021-06-11T14:05:39Z")

</div>

Getting :

❌ Script Error  
⚠ ReferenceError: require is not defined

---

<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:** [June 11, 2021, 4:17pm UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/32 "2021-06-11T16:17:23Z")

</div>

Fredrik was looking for a way to fix it in an app. Homeyscript is doomed.

---

<div class="post-metadata">

**Author:** ![Usstan](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/usstan/32/56813_2.png) [@Usstan](https://community.homey.app/u/Usstan)\
**Post date:** [July 15, 2022, 9:40am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/34 "2022-07-15T09:40:51Z")

</div>

Was looking for a solution for getting local time and happened on this thread. But didn’t get the suggestions to work, so after a little more research

const sys = await Homey.system.getInfo();  
var time = new Date(); //  
// for me, I want it in 24h and just hour and minute, so using Norwegian locale and timezone.  
var output = time.toLocaleTimeString(‘nb-NO’,{hour: ‘numeric’, minute: ‘numeric’, timeZone: sys.timezone})

---

<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 15, 2022, 10:02am UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/35 "2022-07-15T10:02:45Z")

</div>

I’m using this:  
let tz = this.homey.clock.getTimezone();  
now = new Date().toLocaleString(this.homey.i18n.getLanguage(),  
{  
hour12: false,  
timeZone: tz,  
hour: “2-digit”,  
minute: “2-digit”,  
day: “2-digit”,  
month: “2-digit”,  
year: “numeric”  
});

I use this.homey.i18n.getLanguage() as timezone, even if it’s only the first part of the zone (en or nb for you).  
toLocaleString is returning the formated local string depoending on the zone/language sind FW 7.4. FW \<7.4 is only returning the “en-US” version.  
But the timezone was waorking in older FWs, too.

---

<div class="post-metadata">

**Author:** ![Martin\_Ericson\_Borgh](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/martin_ericson_borgh/32/80256_2.png) [@Martin\_Ericson\_Borgh](https://community.homey.app/u/Martin_Ericson_Borgh)\
**Post date:** [July 12, 2023, 10:04pm UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/36 "2023-07-12T22:04:22Z")

</div>

Really useful. Thx! Edited your example to be for Sweden and added code for extraction of local year, month, day, hour etc. Maybe its useful for someone, maybe im klicking in open doors… =)

const sys = await Homey.system.getInfo();  
var time = new Date(); //

//I want it in 24h and just hour and minute, so using Swedish locale and timezone.  
//All below variables are strings n.b.  
var ar = time.toLocaleDateString(‘sv-SE’,{year: ‘numeric’, timeZone: sys.timezone})  
var manad = time.toLocaleDateString(‘sv-SE’,{month: ‘numeric’, timeZone: sys.timezone})  
var dag = time.toLocaleDateString(‘sv-SE’,{day: ‘numeric’, timeZone: sys.timezone})  
var timme = time.toLocaleTimeString(‘sv-SE’,{hour: ‘numeric’, timeZone: sys.timezone})  
var minut = time.toLocaleTimeString(‘sv-SE’,{minute: ‘numeric’, timeZone: sys.timezone})

//Displaying the extracted variables on the console  
log('Lokalt år: ',ar); //log(‘Lokalt år är typ:’,typeof(ar))  
log(‘Lokal månad:’, manad); //log(‘Lokal månad är typ:’,typeof(manad));  
log(‘Lokal dag:’, dag);  
log(‘Lokal timme:’, timme);  
log(‘Lokal minut:’, minut);  
//Displaying time zone of the Homey  
log(‘Tidzon:’,sys.timezone);

---

<div class="post-metadata">

**Author:** ![Paul\_Nas](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/paul_nas/32/80581_2.png) [@Paul\_Nas](https://community.homey.app/u/Paul_Nas)\
**Post date:** [November 6, 2023, 12:56pm UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/37 "2023-11-06T12:56:55Z")

</div>

I am struggling with the same issue: you can get the local time from Homey with some effort but then you still have to account for summer/winter time. All in all there is too much scope for breakage. So I thought: why not outsource all this trouble? I found [https://worldtimeapi.org/](https://worldtimeapi.org/) which you can have determine your time zone based on your public IP address for the ultimate in low-effort time services in your scripts and flows. While the API might not be totally reliable (as they state on their website), it should be possible to get a result at least some of the time and calculate the time difference from UTC based off that and just store the time difference. Then you can calculate time (with or without DST as appropriate) reliably.

---

<div class="post-metadata">

**Author:** ![Arie\_J\_Godschalk](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/arie_j_godschalk/32/63027_2.png) [@Arie\_J\_Godschalk](https://community.homey.app/u/Arie_J_Godschalk)\
**Post date:** [November 6, 2023, 4:04pm UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/38 "2023-11-06T16:04:48Z")

</div>

@Paul_Nas You can use the Better Logic Library app.  
It has many ways of dealing with dates in you (given) timezone.  
No need for external APIs 😉

---

<div class="post-metadata">

**Author:** ![Paul\_Nas](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/paul_nas/32/80581_2.png) [@Paul\_Nas](https://community.homey.app/u/Paul_Nas)\
**Post date:** [November 7, 2023, 2:48pm UTC](https://community.homey.app/t/local-time-in-homeyscript/33338/39 "2023-11-07T14:48:13Z")

</div>

You’re right @Arie_J_Godschalk and I have found new love for BLL, thanks for creating it!

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