Hi there,
I’m trying to setup a Homeyscript that would chat what hour it is and then perform certain action depending on the hour. If I run
const now = new Date();
const currentHour = now.getHours();
I get the UTC current hour but not my local hour here in Finland.
I’ve tried following:
//set desiredLocale to Finland
const desiredLocale = 'fi-FI';
// Create a new Date object
const now = new Date();
// Get the time string in the specified locale
const timeInLocale = now.toLocaleTimeString(desiredLocale);
But the timeInLocale still returns the UTC time?
When I run the code in javascript emulator it works fine? Any Idea why HomeyScript doesn’t localize the time for me?
Thanks
Use the Better Logic Library app for local datetimes.
Thanks Arie, but that’s not really what I was looking for. The solution I want to build seems to need the use of HomeyScript, but I would need to get the local time in the homeyScript, but for some reason the HomeyScript doesn’t return the locale.
Or course I can always add the +2 or +3 depending on if it is summer time or winter time to the time returned by Date(), but the problem is that then I need to update that time always when the Daylight sving starts/ends… For that reason I’d like to get the localization to work, and not have to worry about “wrong timezone”.
toLocaleTimeString()
by itself only formats a time string according to the current locale, not the current timezone. For that, see this post.
You can call the date function in HomeyScript through the api from BLL.