# Trying to convert string to time

**URL:** https://discourse.nodered.org/t/trying-to-convert-string-to-time/71915
**Category:** General
**Created:** [9 December 2022 12:40 UTC](https://discourse.nodered.org/t/trying-to-convert-string-to-time/71915 "2022-12-09T12:40:17Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)
#### Post date: [9 December 2022 12:40 UTC](https://discourse.nodered.org/t/trying-to-convert-string-to-time/71915/1 "2022-12-09T12:40:17Z")

</div>

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:

```auto
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

```auto
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

```

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [9 December 2022 12:47 UTC](https://discourse.nodered.org/t/trying-to-convert-string-to-time/71915/2 "2022-12-09T12:47:59Z")

</div>

This should work

```auto
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/Number#return_value)

---

<div class="post-metadata">

### Author: ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)
#### Post date: [9 December 2022 13:05 UTC](https://discourse.nodered.org/t/trying-to-convert-string-to-time/71915/3 "2022-12-09T13:05:21Z")

</div>

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

```auto
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

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/7/77687d67cfba7e9abb207a1dc8785a0e94c57ea9.png)

---

<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: [9 December 2022 13:49 UTC](https://discourse.nodered.org/t/trying-to-convert-string-to-time/71915/4 "2022-12-09T13:49:23Z")

</div>

> [@Mikee1234K](#):
>
> ```auto
> let change = new Number(lastchange); //1670584440811
> let result = new Date(change);
> 
> ```

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

---

<div class="post-metadata">

### Author: ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)
#### Post date: [9 December 2022 13:52 UTC](https://discourse.nodered.org/t/trying-to-convert-string-to-time/71915/5 "2022-12-09T13:52:19Z")

</div>

Oooops. Even with reading glasses on I missed that. 😳

---

<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: [9 December 2022 14:05 UTC](https://discourse.nodered.org/t/trying-to-convert-string-to-time/71915/6 "2022-12-09T14:05:47Z")

</div>

> [@Mikee1234K](#):
>
> `/Date(1670584440811+0000)`

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

---

<div class="post-metadata">

### Author: ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)
#### Post date: [9 December 2022 14:10 UTC](https://discourse.nodered.org/t/trying-to-convert-string-to-time/71915/7 "2022-12-09T14:10:07Z")

</div>

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 😃

---

<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: [9 December 2022 15:59 UTC](https://discourse.nodered.org/t/trying-to-convert-string-to-time/71915/8 "2022-12-09T15:59:21Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Mikee1234K](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikee1234k/32/70283_2.png) [@Mikee1234K](https://discourse.nodered.org/u/Mikee1234K)
#### Post date: [9 December 2022 16:01 UTC](https://discourse.nodered.org/t/trying-to-convert-string-to-time/71915/9 "2022-12-09T16:01:20Z")

</div>

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

---

<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: [9 December 2022 16:17 UTC](https://discourse.nodered.org/t/trying-to-convert-string-to-time/71915/10 "2022-12-09T16:17:44Z")

</div>

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.

---

<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: [7 February 2023 16:17 UTC](https://discourse.nodered.org/t/trying-to-convert-string-to-time/71915/11 "2023-02-07T16:17:52Z")

</div>

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