# Time Picker in ui\_form node sends wrong time in output payload

**URL:** <https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296>\
**Category:** Dashboard\
**Tags:** home-assistant\
**Created:** [26 November 2022 18:08 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296 "2022-11-26T18:08:25Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [27 November 2022 19:07 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296/42 "2022-11-27T19:07:49Z")

</div>

Changing the type will of course break existing users…

---

<div class="post-metadata">

**Author:** ![Tilerdon](https://avatars.discourse-cdn.com/v4/letter/t/ce73a5/32.png) [@Tilerdon](https://discourse.nodered.org/u/Tilerdon)\
**Post date:** [27 November 2022 19:20 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296/43 "2022-11-27T19:20:59Z")

</div>

can you explain that please @dceejay ?

---

<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:** [27 November 2022 19:24 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296/44 "2022-11-27T19:24:54Z")

</div>

Then maybe a note in the help section may help avoid confusion.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [27 November 2022 19:29 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296/45 "2022-11-27T19:29:57Z")

</div>

> [@Tilerdon](#):
>
> can you explain

@e1cid suggested changed the output format from the current time object to raw milliseconds - so that users could then convert it how they liked... This would break any existing user of the node that it is working for until they then add the necessary conversion for their application. We don't really want to do that unless we really have to and would mean a major bump to dashboard from version 3 to version 4..

---

<div class="post-metadata">

**Author:** ![Tilerdon](https://avatars.discourse-cdn.com/v4/letter/t/ce73a5/32.png) [@Tilerdon](https://discourse.nodered.org/u/Tilerdon)\
**Post date:** [27 November 2022 20:46 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296/46 "2022-11-27T20:46:50Z")

</div>

Thank you @dceejay . I totally understand that.

I found a code snippet and modified and added it to the function which I'm developing to process the form data.

_var dateTime = msg.payload.Start_

_var myDate = new Date(msg.payload.Start);_

_var offset = myDate.getTimezoneOffset() \* 60 \* 1000;_

_var withOffset = myDate.getTime();_

_var withoutOffset = withOffset - offset;_

_node.warn(withOffset);_

_node.warn(withoutOffset);_

_duration = withOffset_

_milliseconds = parseInt(( duration % 1000) / 100)_

_seconds = Math.floor((duration / 1000) % 60)_

_minutes = Math.floor((duration / (1000 \* 60)) % 60)_

_hours = Math.floor((duration / (1000 \* 60 \* 60)) % 24)_

_mess = hours + ":" + minutes + ":" + seconds_

_node.warn(mess)_

_duration = withoutOffset_

_milliseconds = parseInt((duration % 1000) / 100)_

_seconds = Math.floor((duration / 1000) % 60)_

_minutes = Math.floor((duration / (1000 \* 60)) % 60)_

_hours = Math.floor((duration / (1000 \* 60 \* 60)) % 24)_

_mess = hours + ":" + minutes + ":" + seconds_

The debug output shows that this works at this moment in time and probably up to 26 March 2023

![Screenshot 2022-11-27 204339](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/b/bb3f01caff51f372d47b1d06631de1e302e48156.png)

I'm not sure if there is any way to test whether this really does work until we really have DST and UTC 00.00 but I think I'll use it until then

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [27 November 2022 21:19 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296/47 "2022-11-27T21:19:47Z")

</div>

You should add a comment node to the tab you are using this on and document what you did and why so in March next year if any issue comes up, you have something to jog your memory.

---

<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 November 2022 21:48 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296/48 "2022-11-27T21:48:36Z")

</div>

In case anyone is interested I have found the cause of the confusion. In 1968, as I now remember, but had forgotten, the UK trialled year round daylight saving time. This trial lasted until 1971 and meant that London was permanently on DST or UTC+1. Hence, in the UK, the start of the epoch was at 01:00 local time.

---

<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 November 2022 21:58 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296/49 "2022-11-27T21:58:52Z")

</div>

> [@E1cid](#):
>
> The timestamp `1970-01-01T16:01:00.000Z` converted to milliseconds is `61260000` which is 17.0166666667 hours. Using `$toMillis()`

Well $toMillis() is wrong as far as I can see. This code gives 57660000, as it should  
`msg.payload = new Date("1970-01-01T16:01:00.000Z").getTime()`

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [27 November 2022 23:13 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296/50 "2022-11-27T23:13:21Z")

</div>

Yes it seems to be the browser returning a date object that includes the time zone UTC+1 (when in uk) The html5 input doesn’t seem to have any options to change that so I’m not sure how we can adjust it.

---

<div class="post-metadata">

**Author:** ![Tilerdon](https://avatars.discourse-cdn.com/v4/letter/t/ce73a5/32.png) [@Tilerdon](https://discourse.nodered.org/u/Tilerdon)\
**Post date:** [28 November 2022 08:05 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296/51 "2022-11-28T08:05:56Z")

</div>

Do you feel that the solution that I proposed above will stand the test of time and still work when we do have DST in effect and that the solution can only be applied at a developer level?

---

<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:** [28 November 2022 09:21 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296/52 "2022-11-28T09:21:37Z")

</div>

> [@Tilerdon](#):
>
> Do you feel that the solution that I proposed above will stand the test of time and still work when we do have DST in effect

I think this is a simpler solution. The code

```auto
const time = new Date(msg.payload.time)
const msecLocal = time.getTime() - time.getTimezoneOffset() * 60000

```

gives the milliseconds since the start of the day local time, from which anything else can be calculated. I think that should work for any time zone, provided the server and browser are configured for the same zone. At the moment my brain has not been able to cope with considering the situation where the two timezones are different.  
I have to go for the moment, but will think about the most efficient way to get the hours/mins/secs from that.

---

<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:** [28 November 2022 10:26 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296/53 "2022-11-28T10:26:02Z")

</div>

> [@Colin](#):
>
> In case anyone is interested I have found the cause of the confusion. In 1968, as I now remember, but had forgotten, the UK trialled year round daylight saving time. This trial lasted until 1971 and meant that London was permanently on DST or UTC+1. Hence, in the UK, the start of the epoch was at 01:00 local time.

I think I already mentioned that in post 32 above.

> [@Colin](#):
>
> Well $toMillis() is wrong as far as I can see.

Is it? or is it handling the year round UK DST in 1970 correctly?

---

<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:** [28 November 2022 14:51 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296/54 "2022-11-28T14:51:43Z")

</div>

> [@E1cid](#):
>
> > [@Colin](#):
> >
> > Well $toMillis() is wrong as far as I can see.
> 
> Is it? or is it handling the year round UK DST in 1970 correctly?

If I put, in a Change node, Set msg.payload To JSONata `$toMillis("1970-01-01T16:01:00.000Z")` I get 57660000, which agrees with Date.getTime(). Where did you see $toMillis() give 61260000 for that timestamp?

---

<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:** [28 November 2022 15:07 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296/55 "2022-11-28T15:07:06Z")

</div>

> [@Colin](#):
>
> the most efficient way to get the hours/mins/secs from that.

In fact it is easier than I thought. This will give the hours and minutes as entered into the time picker

```auto
const time = new Date(msg.payload.Start)
// this is the number of milliseconds into the day, if needed
//const msecLocal = time.getTime() - time.getTimezoneOffset() * 60000
const hours = time.getHours()
const mins = time.getMinutes()
msg.payload = `${hours.toString().padStart(2,"0")}:${mins.toString().padStart(2,"0")}`
return msg;

```

**[Edit]** Corrected code to pick up from msg.payload.Start

---

<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:** [29 November 2022 11:03 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296/56 "2022-11-29T11:03:38Z")

</div>

@Tilerdon does my simpler solution work for you?

Anyone else, can you confirm whether it works ok with a timezone other than uk?

I am pretty sure it will not work if the browser and server timezones are different, and I don't think there is any way to make it work, unless someone can think of anything. The problem is that it is necessary to know the browser timezone.

It would have been better if the node sent the time as received from the browser, including the the TZ information, but there is not much that can be done about that now. I can't think of a way of fixing it and keeping it backwards compatible, other than having a new option to change the format.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [29 November 2022 11:29 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296/57 "2022-11-29T11:29:48Z")

</div>

@Colin the code works fine with a modification to the first line in order to work with @Tilerdon's flow - `msg.payload.time` should be `msg.payload.Start`

![Screen Shot 2022-11-29 at 6.27.07 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/b/8be3db5cf292eb7e6c7024aa72a1e296b9e99e1f.png)

 ![Screen Shot 2022-11-29 at 6.37.26 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/9/8941af0ace1808cda28cc17ff6f3d604175b0faf.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:** [29 November 2022 11:39 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296/58 "2022-11-29T11:39:42Z")

</div>

> [@zenofmud](#):
>
> `msg.payload.time` should be `msg.payload.Start`

Oh yes, I was testing with a reduced example.  
I will correct my post.  
Thanks.

---

<div class="post-metadata">

**Author:** ![Tilerdon](https://avatars.discourse-cdn.com/v4/letter/t/ce73a5/32.png) [@Tilerdon](https://discourse.nodered.org/u/Tilerdon)\
**Post date:** [29 November 2022 11:53 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296/59 "2022-11-29T11:53:43Z")

</div>

> [@Colin](#):
>
> ```auto
> const time = new Date(msg.payload.time)
> // this is the number of milliseconds into the day, if needed
> //const msecLocal = time.getTime() - time.getTimezoneOffset() * 60000
> const hours = time.getHours()
> const mins = time.getMinutes()
> msg.payload = `${hours.toString().padStart(2,"0")}:${mins.toString().padStart(2,"0")}`
> 
> ```

Yes @Colin that works fine thank you. I agree that it does look simpler than the other code that I picked up. Not quite sure about the use of $ however but that is a matter for me to resolve at some other time.

---

<div class="post-metadata">

**Author:** ![Tilerdon](https://avatars.discourse-cdn.com/v4/letter/t/ce73a5/32.png) [@Tilerdon](https://discourse.nodered.org/u/Tilerdon)\
**Post date:** [29 November 2022 11:57 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296/60 "2022-11-29T11:57:43Z")

</div>

Thank you all for your help and the interesting issues raised by 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:** [29 November 2022 13:30 UTC](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296/61 "2022-11-29T13:30:19Z")

</div>

> [@Tilerdon](#):
>
> Not quite sure about the use of $ however

```auto
msg.payload = `${hours.toString().padStart(2,"0")}:${mins.toString().padStart(2,"0")}`

```

That is just using [string template literals](https://www.w3schools.com/js/js_string_templates.asp). The above is exactly the same as

```auto
msg.payload = hours.toString().padStart(2,"0") + ":" + mins.toString().padStart(2,"0")

```

but is arguably easier to code and to understand. It doesn't make much difference in this case, but if you are trying to build a string with quotes and double quotes in it then the traditional method can get horribly complex.

[Previous page](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296.md?page=2)

[Next page](https://discourse.nodered.org/t/time-picker-in-ui-form-node-sends-wrong-time-in-output-payload/71296.md?page=4)
