Local time in HomeyScript

Extract it from “date human”.

Ehhh?

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…

Neither am I :roll_eyes:

1 Like
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.
1 Like

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 :frowning:

*: 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.

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

Yes, it was :smiley:

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

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

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

Perhaps it’s easier to use luxon:

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());
1 Like

Getting :

:x: Script Error
:warning: ReferenceError: require is not defined

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

1 Like

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})

1 Like

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.

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);

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/ 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.

@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 :wink:

1 Like

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

1 Like