Adjusting for timezone to display the time on an analogue clock

Edit - "function node" and "template node" swapped over.

If I use a function node I can adjust the UTC timestamp from new Date() to account for BST

let d = new Date();                           // eg 2021-10-02T14:11:35.000Z
let ts = d.getTime();                         // current timestamp in UTC
let offset = (d.getTimezoneOffset() * 60000); // BST getTimezoneOffset returns -60 minutes
let adjustedts = (ts - offset);               // Adjust the timestamp
let adjusteddate = new Date(adjustedts);      // eg 2021-10-02T15:11:35.000Z
msg.payload = adjusteddate;
return msg;

But when I transfer that code to a script in a template node it doesn't work, the offset always seems to be 0

<script>
(function(scope) {
  scope.$watch('msg', function(msg) {
    if (msg) {
        let d = new Date();                           // eg 2021-10-02T14:11:35.000Z
        console.log("UTC hour ", d.getHours());
        let ts = d.getTime();                         // current timestamp in UTC
        let offset = (d.getTimezoneOffset() * 60000); // BST getTimezoneOffset returns -60 minutes
        console.log("offset", offset);
        let adjustedts = (ts - offset);               // Adjust the timestamp
        let adjusteddate = new Date(adjustedts);      // eg 2021-10-02T15:11:35.000Z

        let sec = adjusteddate.getSeconds();
        let mins = adjusteddate.getMinutes();
        let hour = adjusteddate.getHours();
        console.log("hour ", hour);
        
        let secDeg = ''+(((sec / 60) * 360) + 90)+'deg'; 
        let minsDeg = ''+(((mins / 60) * 360) + ((sec/60)*6) + 90)+'deg';         
        let hourDeg = ''+(((hour / 12) * 360) + ((mins/60)*30) + 90)+'deg';
        
        document.getElementById('gauge_'+scope.$id).style.setProperty('--time-hour', hourDeg);
        document.getElementById('gauge_'+scope.$id).style.setProperty('--time-minute', minsDeg);
        document.getElementById('gauge_'+scope.$id).style.setProperty('--time-second', secDeg);
    }
   
  });
})(scope);
</script>

The three console.log() statements give
Untitled 1

I'm guessing this is something to do with scope (which I don't understand in javascript)?
Can anyone point me in the right direction?

Is node-red and the client browser on the same machine and do they have the same timezone setting?

Node-red is on a Raspberry Pi - date shows Sat 2 Oct 16:16:06 BST 2021
The browser is on WIndows 10 and the time shows as 16:16:20 Sat 2 October 2021 (it automatically adjusts the clock GMT/BST)

Ah. The function code runs on the Pi, the template on the PC, presumably.
Does getTimezoneOffset() work in a browser on a PC?

Yes.

On PI, What does cat /etc/timezone show?

On windows, what timezone is set?

pi $ cat /etc/timezone
Europe/London

PC
Untitled 2

Hmm my browser clearly doesn't think summer time applies here.
My PC knows it's 20:15 but my browser says d.getHours() and d.getUTCHours() are both 19.

Nailed it!
about:config privacy.resistFingerprinting was set to true.
Set it back to default, restarted Firefox (cleared the cache) and it's working now.

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