# Daylight saving - best approach

**URL:** <https://discourse.nodered.org/t/daylight-saving-best-approach/60403>\
**Category:** General\
**Created:** [27 March 2022 12:33 UTC](https://discourse.nodered.org/t/daylight-saving-best-approach/60403 "2022-03-27T12:33:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![abroad](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@abroad](https://discourse.nodered.org/u/abroad)\
**Post date:** [27 March 2022 12:33 UTC](https://discourse.nodered.org/t/daylight-saving-best-approach/60403/1 "2022-03-27T12:33:09Z")

</div>

I've built my own scheduler in node-red for controlling stuff around the house.  
The basic data structure is an array of entries like:  
[actionID, time, recurrence, parameters]  
[10, 1648395000000, 86400000,{which:"fish", state:"on"}]  
Above should turn on the fish tank light at 16:30 every day.  
The time is stored as MS since 1-1-1970 00:00:00 UTC.  
Today the clocks here in London went forward to summer time; but all my events need to respect local time.  
I can fudge the schedule by adding 60000 onto all the times (then taking it off in 6 months time); but I'd like to do the job properly. So if you schedule something at 22:00 it happens at 22:00 all year long.  
Any suggestions of how best to implement this?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [27 March 2022 14:06 UTC](https://discourse.nodered.org/t/daylight-saving-best-approach/60403/2 "2022-03-27T14:06:26Z")

</div>

You can use  
`const offset = new Date().getTimezoneOffset()`  
Which will give you the current timezone offset in minutes. You can use that to offset the time you are comparing against.

---

<div class="post-metadata">

**Author:** ![JayNaire](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaynaire/32/17343_2.png) [@JayNaire](https://discourse.nodered.org/u/JayNaire)\
**Post date:** [28 March 2022 14:49 UTC](https://discourse.nodered.org/t/daylight-saving-best-approach/60403/3 "2022-03-28T14:49:26Z")

</div>

I found using "hh:mm", not timestamps much easier to deal with when constructing daily schedules.  
Schedule:  
`[{"id":0,"to":"02:00","temp":19.5}, {"id":1,"to":"10:00","temp":20.5}, ...] //id not essential if array is always sorted`

```auto
function getTemp(sched) {
    //return the scheduled temp for this time of day
    const now = moment();
    let test = moment();
    return sched.find(el => {
        const hhmm = el.to.split(":");
        //change midnight this morning (00:00) to midnight tonight (23:59:59)
        if (el.to == "00:00") test.hour(23).minute(59).second(59);
        else test.hour(hhmm[0]).minute(hhmm[1]);
        return now.isSameOrBefore(test, "second");
    }).temp; 
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [27 May 2022 14:49 UTC](https://discourse.nodered.org/t/daylight-saving-best-approach/60403/4 "2022-05-27T14:49:35Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
