Date Picker adding odd time

HI - I've added a date picker node to my dashboard. When I look at the output and convert that unix timestamp to a real date time (e.g. on unixtimestamp.com), it always seems to set the time to 9:10am (UTC). I was assuming that the date picker would output the timestamp with the time set to midnight.

What I am trying to do is build a dashboard where you can specify the start date/time and end date/time for a filter - date picker gives date, then I have dropdowns allowing choice of hour (in 24 hour format). I then simply add the selected hours to the selected date. But since the timestamp from the date picker already has hours set (at 9:10 am), of course the output is then wrong.

What is causing the date picker to set this odd hour/minute?

Could you export your flow and attach it to a reply - see this thread for instructions

Are you injecting anything into the date picker? If an inject node is set to timestamp and you inject that into the date picker I think it will be set to your current time, and remain like that, until you inject something else.

HI sorry had been on a short vacation!

OK - so nothing being injected. I have just created a basic flow with a date picker going to a debug.

[
{
"id": "7f86ea3e.47f1a4",
"type": "tab",
"label": "Flow 4"
},
{
"id": "496ddd9e.1fb564",
"type": "ui_date_picker",
"z": "7f86ea3e.47f1a4",
"name": "",
"label": "date",
"group": "d9f24c8.aec10b",
"order": 0,
"width": 0,
"height": 0,
"passthru": true,
"topic": "",
"x": 180,
"y": 60,
"wires": [
[
"961a0b42.4c34f8"
]
]
},
{
"id": "961a0b42.4c34f8",
"type": "debug",
"z": "7f86ea3e.47f1a4",
"name": "",
"active": true,
"console": "false",
"complete": "false",
"x": 300,
"y": 60,
"wires":
},
{
"id": "d9f24c8.aec10b",
"type": "ui_group",
"z": "",
"name": "Data",
"tab": "d6e76239.d1ed8",
"order": 3,
"disp": true,
"width": "12",
"collapse": false
},
{
"id": "d6e76239.d1ed8",
"type": "ui_tab",
"z": "",
"name": "Historian",
"icon": "dashboard",
"order": 3
}
]

Unfortunately your flow isn't currently importable.Please read the following post How to share code or flow json and then edit the above message.

[
    {
        "id": "496ddd9e.1fb564",
        "type": "ui_date_picker",
        "z": "7f86ea3e.47f1a4",
        "name": "",
        "label": "date",
        "group": "d9f24c8.aec10b",
        "order": 0,
        "width": 0,
        "height": 0,
        "passthru": true,
        "topic": "",
        "x": 180,
        "y": 60,
        "wires": [
            [
                "961a0b42.4c34f8"
            ]
        ]
    },
    {
        "id": "961a0b42.4c34f8",
        "type": "debug",
        "z": "7f86ea3e.47f1a4",
        "name": "",
        "active": true,
        "console": "false",
        "complete": "false",
        "x": 300,
        "y": 60,
        "wires": []
    },
    {
        "id": "d9f24c8.aec10b",
        "type": "ui_group",
        "z": "",
        "name": "Data",
        "tab": "d6e76239.d1ed8",
        "order": 3,
        "disp": true,
        "width": "12",
        "collapse": false
    },
    {
        "id": "d6e76239.d1ed8",
        "type": "ui_tab",
        "z": "",
        "name": "Historian",
        "icon": "dashboard",
        "order": 3
    }
]

If you add a function node with

msg.payload = new Date(msg.payload)
return msg;

then for me when I select a date it is providing that date but setting the time to the time the node was deployed, or first displayed, or something like that. I would have thought that was at least a deficiency if not a bug.
However, as a workaround, if you just want the date then all you have to do is to clear the time of day before adding in your extra offset.

ok that makes sense - could not understand why the time was being set as would have thought it should have been set to midnight on the date selected. As you say, not ideal but not necessarily a bug.

Colin, can you explain how to achieve "clearing the time of day" from the datepicker payload? The only way I can think to do that is to extract the time in the payload in hours,mins,secs.ms, converting it all to milliseconds and then subtract it. Is there an easier way? I'm still learning and probably missing something obvious.
I'm not sure what the use case is for including the semi-random time (in UTC), since the datepicker Node only chooses Date and not time. In my case I'm using it to select records from a SQLITE database by Date, and this seems awkward.
Thanks in advance for any guidance.

Have a look at the docs for the Date object (https://www.w3schools.com/jsref/jsref_obj_date.asp) and you will see functions to set the hours, mins etc. You can call those to clear the relevant parts.

I think I've figured it out, and in the process learned a lot more about moving between timestamps and time-strings and extracting the time/date components. Here's a summary of what I found:

  1. I compared the output from the datepicker UI to the output from a form UI with a Date. The Picker adds the last deployment time of day offset to the picked date. Can't see what the use case is for this, and it's a pain in my application. The form UI always returns the UTC date with no time offset, i.e. midnight.
  2. Removing the time offset from the Picker seems like a lot of code, unless there's an easier way.
  3. I guess this is just my lack of javascript knowledge, but it took a while to work out that the Date() function needs to be prefixed with 'new' every time it's used.

My application is using these UI nodes to SELECT data from a SQLITE database, and since the date functions of SQL default to data stored in UTC, the form UI seems much more aligned.

Any feedback or suggestions are welcome.

[{"id":"5a76dea2.0a70c","type":"tab","label":"Date Tests","disabled":false,"info":""},{"id":"d22d96ed.f75f78","type":"ui_date_picker","z":"5a76dea2.0a70c","name":"","label":"date","group":"7d3e55e4.2a3e1c","order":5,"width":0,"height":0,"passthru":true,"topic":"","x":130,"y":140,"wires":[["355ad7d5.9f2168","2218eb0f.eedc04","814d0dcd.9cb78","159a9ea9.69e0f1","a5700c9.daf29f"]]},{"id":"d23ed944.c8c778","type":"ui_text","z":"5a76dea2.0a70c","group":"7d3e55e4.2a3e1c","order":1,"width":0,"height":0,"name":"","label":"Picker Date","format":"{{msg.payload}}","layout":"row-spread","x":610,"y":60,"wires":[]},{"id":"814d0dcd.9cb78","type":"debug","z":"5a76dea2.0a70c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":690,"y":160,"wires":[]},{"id":"2b73dc3f.b3c5c4","type":"ui_form","z":"5a76dea2.0a70c","name":"","label":"","group":"aee7541a.69eb18","order":4,"width":0,"height":0,"options":[{"label":"Date Form","value":"Date_Form","type":"date","required":true,"rows":null}],"formValue":{"Date_Form":""},"payload":"","submit":"submit","cancel":"cancel","topic":"","x":90,"y":400,"wires":[["5c075167.c4849"]]},{"id":"13ada6c6.ac9e39","type":"ui_text","z":"5a76dea2.0a70c","group":"aee7541a.69eb18","order":1,"width":0,"height":0,"name":"","label":"Form Text","format":"{{msg.payload}}","layout":"row-spread","x":530,"y":340,"wires":[]},{"id":"189f3c5b.19a1d4","type":"debug","z":"5a76dea2.0a70c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":770,"y":520,"wires":[]},{"id":"355ad7d5.9f2168","type":"function","z":"5a76dea2.0a70c","name":"Date(msg.payload)","func":"msg.payload = new Date(msg.payload);\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":60,"wires":[["d23ed944.c8c778","814d0dcd.9cb78"]]},{"id":"2218eb0f.eedc04","type":"ui_text","z":"5a76dea2.0a70c","group":"7d3e55e4.2a3e1c","order":1,"width":0,"height":0,"name":"","label":"Picker Timestamp","format":"{{msg.payload}}","layout":"row-left","x":430,"y":120,"wires":[]},{"id":"5c075167.c4849","type":"function","z":"5a76dea2.0a70c","name":"Convert form to Time/Date","func":"var msg2 = {};\nvar msg3 = {};\nmsg2.topic = \"millis\";\n\nvar dateNow = msg.payload.Date_Form;\nmsg.payload = dateNow;\n\nvar timemillis = new Date(dateNow).getTime();\nmsg2.payload = timemillis;\n\nvar localetime = new Date(dateNow).toLocaleTimeString();\nmsg3.payload = localetime;\n\nreturn [msg,msg2,msg3];","outputs":3,"noerr":0,"x":320,"y":400,"wires":[["13ada6c6.ac9e39"],["5dfeadf7.0186c4"],["2c2b4e78.dc53a2"]]},{"id":"5dfeadf7.0186c4","type":"ui_numeric","z":"5a76dea2.0a70c","name":"","label":"Form Timestamp","tooltip":"","group":"aee7541a.69eb18","order":2,"width":0,"height":0,"wrap":false,"passthru":true,"topic":"","format":"{{value}}","min":0,"max":10,"step":1,"x":630,"y":380,"wires":[["189f3c5b.19a1d4"]]},{"id":"159a9ea9.69e0f1","type":"function","z":"5a76dea2.0a70c","name":"Date->Time","func":"var timeofday = new Date(msg.payload).toLocaleTimeString();\nmsg.payload = timeofday;\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":200,"wires":[["b52ba74d.734348"]]},{"id":"b52ba74d.734348","type":"ui_text","z":"5a76dea2.0a70c","group":"7d3e55e4.2a3e1c","order":1,"width":0,"height":0,"name":"","label":"Local Time","format":"{{msg.payload}}","layout":"row-left","x":490,"y":200,"wires":[]},{"id":"2c2b4e78.dc53a2","type":"ui_text","z":"5a76dea2.0a70c","group":"aee7541a.69eb18","order":3,"width":0,"height":0,"name":"","label":"Local Time","format":"{{msg.payload}}","layout":"row-spread","x":630,"y":440,"wires":[]},{"id":"a5700c9.daf29f","type":"function","z":"5a76dea2.0a70c","name":"Adjust date to midnight UTC","func":"//Remove time of day offset from Date Picker output\nvar timestring = new Date(msg.payload);\n var dateplustime = new Date(timestring).getTime();\n var hours = new Date(timestring).getUTCHours();\n var seconds= new Date(timestring).getUTCSeconds();\n var minutes= new Date(timestring).getUTCMinutes();\n var millis= new Date(timestring).getUTCMilliseconds();\n var offset = millis+1000*(seconds+minutes*60+hours*3600);\n var UTCOffsetMins = new Date(timestring).getTimezoneOffset();\n \n\nmsg.payload = dateplustime - offset + (UTCOffsetMins * 60000);\n\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":260,"wires":[["3fe72c3b.93fa94"]]},{"id":"3fe72c3b.93fa94","type":"ui_text","z":"5a76dea2.0a70c","group":"7d3e55e4.2a3e1c","order":3,"width":0,"height":0,"name":"","label":"Adjusted Timestamp","format":"{{msg.payload}}","layout":"row-spread","x":740,"y":260,"wires":[]},{"id":"7d3e55e4.2a3e1c","type":"ui_group","z":"","name":"Date Picker","tab":"7ebce11d.a7c9e","order":1,"disp":true,"width":"6","collapse":false},{"id":"aee7541a.69eb18","type":"ui_group","z":"","name":"Form Date","tab":"7ebce11d.a7c9e","order":2,"disp":true,"width":"6","collapse":false},{"id":"7ebce11d.a7c9e","type":"ui_tab","z":"","name":"Date Tests","icon":"dashboard","order":9,"disabled":false,"hidden":false}]

I am not sure exactly what you need, but iIf you want to get a timestamp for the start of the local timezone day from the date time picker you can do

let startOfDayUtc = Math.floor(msg.payload/(24*3600*1000))*24*3600*1000
let tzOffset_ms = (new Date(startOfDayUtc)).getTimezoneOffset() * 60000
msg.payload = new Date(startOfDayUtc + tzOffset_ms)
return msg;

If you want it in ticks rather than a Date then replace the penultimate line with

msg.payload = startOfDayUtc + tzOffset_ms
1 Like

Thanks Colin. A lot simpler than mine, of course! I still think it would be nice if UI_date_picker output had an option to deliver it like this, without the somewhat spurious time-of-day-node-deployed, and therefore in line with UI_form for Date fields, which does exactly this. I think going forward I will use UI_form.

I agree. I don't know whether anyone ever submitted an issue against the node.