UI_form formElement Date off -1 day

I need two dates as start and endpoint for my graphs from the database.
I use date picker Form Element type Date
Now I found out that if I select 2020-05-02 the resulting output is 2020-05-01T22:00:00Z.
I now crop off the T22:00:00Z, I don't need the time, that is added later.
Any idea how to get to the date I selected?

What time zone are you (GMT +?)

What timezone is the node-red sever? (Same or different?)

And what time is it now?

Ps, show us your code how you remove the time part.

Lastly, put a debug node after the form control and show what comes into debug node.

EDIT...

OK, I'm bored of waiting.

The problem is - there is no problem. The time you are seeing in is UTC. You own timezone is likely GMT+2?

Here is an example (note time formStart and formEnd times are -1h (AKA UTC time) - I am in GMT+1)
Then note how the proofs and the starttoString/endToString are correct.

This is the function node code if you want it...

function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) 
        month = '0' + month;
    if (day.length < 2) 
        day = '0' + day;

    return [year, month, day].join('-');
}

let dStart = new Date(msg.payload.formStart);
//proof time is normal (its a timezone thing)
node.warn(["proof start date is ok - yyyy mm dd hh mm ss",
    dStart.getFullYear(),
    dStart.getMonth()+1,
    dStart.getDate(),
    dStart.getHours(),
    dStart.getMinutes(),
    dStart.getSeconds()
    ]);
msg.payload.startToString = dStart.toString();//default toString    
msg.payload.startDateIsoformat = formatDate(dStart); //iso format date only

let dEnd = new Date(msg.payload.formEnd);
//proof time is normal (its a timezone thing)
node.warn(["proof end date is ok - yyyy mm dd hh mm ss",
    dEnd.getFullYear(),
    dEnd.getMonth()+1,
    dEnd.getDate(),
    dEnd.getHours(),
    dEnd.getMinutes(),
    dEnd.getSeconds()
    ]);
msg.payload.endToString = dEnd.toString();//default toString
msg.payload.endDateIsoformat = formatDate(dEnd); //iso format date only
return msg;

Sorry, I was called away by someone with a computer problem.
I am in MEZT time zone and it is running on a RBPi3b.

[{"id":"6e3ea82b.1fc4d8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"1fb84481.252c73","type":"ui_form","z":"6e3ea82b.1fc4d8","name":"Test","label":"","group":"47dd1af2.e7db4c","order":12,"width":0,"height":0,"options":[{"label":"Van","value":"Start","type":"date","required":true,"rows":null},{"label":"t/m","value":"Eind","type":"date","required":true,"rows":null}],"formValue":{"Start":"","Eind":""},"payload":"","submit":"Test","cancel":"","topic":"","x":130,"y":120,"wires":[["d7a0a766.f62108","dc1d20d2.9ef31"]]},{"id":"d7a0a766.f62108","type":"debug","z":"6e3ea82b.1fc4d8","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":290,"y":80,"wires":[]},{"id":"f7ccd27e.fa36b8","type":"debug","z":"6e3ea82b.1fc4d8","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":450,"y":80,"wires":[]},{"id":"dc1d20d2.9ef31","type":"change","z":"6e3ea82b.1fc4d8","name":"chop_off","rules":[{"t":"change","p":"payload.Start","pt":"msg","from":"T22:00:00.000Z","fromt":"str","to":"","tot":"str"},{"t":"change","p":"payload.Eind","pt":"msg","from":"T22:00:00.000Z","fromt":"str","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":120,"wires":[["f7ccd27e.fa36b8"]]},{"id":"47dd1af2.e7db4c","type":"ui_group","z":"","name":"Historie","tab":"d77eb30.bbb0a5","order":2,"disp":true,"width":"6","collapse":false},{"id":"d77eb30.bbb0a5","type":"ui_tab","z":"","name":"Grafieken","icon":"dashboard","order":2,"disabled":false,"hidden":false}]


This is what comes out.

The debug window is showing you UTC time. Its actually correct, it's just your interpretation that is wrong.

Study the image I posted.

Try the function I posted.

Ok, but when I see a calendar and pick a date I expect to get the date I pick and not the zulu time from the 0:00 time of the day I chose. That is not logical.

Oh sorry, I made a mistake. The problem is not in the datepicker, it is in the form.
The date picker gives the right date if you ask for a date.
But as I wanted 2 dates I used the form and selected the date field from there.
Sorry I made a mistake.but I am a little confused by all the possibillities node-RED has, but I am glad with it.

1 Like

I think it's to do with there being no specific date only object/type in JavaScript.

So to be 100% compatible, UTC (Zulu) is the base representation.

Its your use of the date object that matters. The date IS correct. Just use toString to get a local string representation or use moment or a custom function like I wrote for you.

Its just how it is. Timezones are a nightmare.

1 Like

Glad you're sorted.

I tried your function and now I get British Summertime.... :roll_eyes:
(and still not the right date)

Show us the debug output of the function.

{"Start":"2020-04-30T22:00:00.000Z","Eind":"2020-05-01T22:00:00.000Z","startToString":"Thu Apr 30 2020 23:00:00 GMT+0100 (British Summer Time)","startDateIsoformat":"2020-04-30","endToString":"Fri May 01 2020 23:00:00 GMT+0100 (British Summer Time)","endDateIsoformat":"2020-05-01"}

Oh everything is running on my rbPi3b with local time GMT+2 (MEZT). I'll check that....

Officially confused. Might be the gin tbh.

Post your flow and I'll take a look tomorrow.

In the mean time, keep plugging away.

Some thoughts...
What timezone is the pi set to (I'm assuming that's where you run node-red)

What timezone is your browser's machine set to (if you're viewing the node-red editor on a different machine)

Error. I logged into the Pi and found out that I did not configure the timezone and that was BST!
I changed that.
My problem is I have 2 Pi's, one is running node-RED and is controlling my heater and as a development system I have a second Pi for test purposes so I don't interfere with my production (ahum) system.
And in my haste I must have forgotten to setup the second one properly.

After a reboot your function gives the correct values.
So this can be transferred to Production Environment :smiley:
Thanks very much

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