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
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?