Trying to convert string to time

I am trying to convert a string to a time. The string is in payload.last_change
and is something like this: "/Date(1670584440811+0000)/"

If I use this to convert the number to the time it works:

var a = new Date(1670584440811);
msg.payload = { "Time": a.toLocaleTimeString('en-UK') };
return msg

Now, I have changed the function to convert (slice) the string then convert to a number. That all works. But if I now enter the variable into the above instead of the number, it doesnt. I am not sure why. new Date(change) is red underlined so the function does not like it

var state = msg.payload.last_change  //"/Date(1670584440811+0000)/"
var lastchange = state.slice(6, 19)  //"1670584440811"
var change = new Number(lastchange);  //1670584440811
var result = new Date(change);

msg.payload = { "Time": result.toLocaleTimeString('en-UK') }; 

return msg

This should work

let state = "/Date(1670584440811+0000)/";
let lastchange = state.slice(6, 19) ; //"1670584440811"
let change = Number(lastchange);  //1670584440811
let result = new Date(change);

msg.payload = { "Time": result.toLocaleTimeString('en-UK') }; 

return msg

The reason Number() constructor - JavaScript | MDN

Now this is weird. After the changes, new Date(change) is still red underlined. I deployed anyway just to see. And it works. But now every time I deploy, I get an error about that function, and the function has a red triangle on it. When I hover over the triangle, it says error in javascript code

let state = msg.payload.last_change;  //"/Date(1670584440811+0000)/"
let lastchange = state.slice(6, 19);  //"1670584440811"
let change = new Number(lastchange);  //1670584440811
let result = new Date(change);


msg.payload = { "Time": result.toLocaleTimeString('en-UK') }; 

return msg

You have not changed the 1st line to Number() instead of new Number.

Oooops. Even with reading glasses on I missed that. :flushed:

Is that 0000 going to change to 0001 when we go to into DST?

Very good question. I dont know. I have only been using Node Red for about a month so did not see summer time. The question is, if it does, and the number before is the GMT time, I would have slice that string, and if its 0001 somehow add 1 hour...
But, by summer time I might have enough understanding to be able to do it :smiley:

It depends on what the device does. It should still give you the ticks in since 1st Jan 1970 UTC, with the +0001 telling you that the timezone is +1 so you can use that when displaying it. In which case, since you are forcing it to display the time in the current UK time zone when you display it then you won't need to do anything.

What device is that strangely formatted time coming from? Are you able to tell it to use a different timezone to test what it does?

What are you doing with the time? If you are storing it for later access you should keep it in UTC until the time comes to display it.

Its coming from Homeseer, my home automation system. I will see what happens after the clocks change, if its not right it shouldnt be too difficult to change. I am displaying the time on my dashboard, to see what time the last change happend. So I can see for example when the setpoint on my Nest thermostst has changed

The reason you shouldn't have to do anything is that a Date object internal storage (the ticks number from your input data) is always in UTC. The timezone in a date object is just a hint about how to display it. Since you are forcing the timezone when you convert it to a string then there should not be a problem.