Get time matching ui_date_picker

Hi
I am trying to store some data alongside the date without the hours (in milliseconds).
I want just the date (without the hour) to match the value given by the ui_date_picker.
I am a beginner, it can be just simple javascript but I`ve trying but didn´t achieve nothing.
I want values like this given by the date picker.
Screenshot_1
I´ll be glad if someone can help me.

I use node-red-contrib-moment

here is an example....

[{"id":"38408627.2b4cda","type":"moment","z":"150b582b.3e6d68","name":"year","topic":"","input":"epoch","inputType":"msg","inTz":"America/New_York","adjAmount":0,"adjType":"days","adjDir":"add","format":"YYYY","locale":"C","output":"year","outputType":"msg","outTz":"America/New_York","x":330,"y":860,"wires":[["16c64f42.d8cb39"]]},{"id":"16c64f42.d8cb39","type":"moment","z":"150b582b.3e6d68","name":"month","topic":"","input":"epoch","inputType":"msg","inTz":"America/New_York","adjAmount":0,"adjType":"days","adjDir":"add","format":"MM","locale":"C","output":"month","outputType":"msg","outTz":"America/New_York","x":450,"y":860,"wires":[["239baeb.17c58d2"]]},{"id":"239baeb.17c58d2","type":"moment","z":"150b582b.3e6d68","name":"day","topic":"","input":"epoch","inputType":"msg","inTz":"America/New_York","adjAmount":0,"adjType":"days","adjDir":"add","format":"DD","locale":"C","output":"day","outputType":"msg","outTz":"America/New_York","x":570,"y":860,"wires":[["4cc6e2d8.4df52c"]]},{"id":"4cc6e2d8.4df52c","type":"moment","z":"150b582b.3e6d68","name":"hour","topic":"","input":"epoch","inputType":"msg","inTz":"America/New_York","adjAmount":0,"adjType":"days","adjDir":"add","format":"HH","locale":"C","output":"hour","outputType":"msg","outTz":"America/New_York","x":690,"y":860,"wires":[["c059c10b.4cf98"]]},{"id":"c059c10b.4cf98","type":"moment","z":"150b582b.3e6d68","name":"minutes","topic":"","input":"epoch","inputType":"msg","inTz":"America/New_York","adjAmount":0,"adjType":"days","adjDir":"add","format":"mm","locale":"C","output":"minutes","outputType":"msg","outTz":"America/New_York","x":820,"y":860,"wires":[["97523b5.7c753c8"]]},{"id":"97523b5.7c753c8","type":"moment","z":"150b582b.3e6d68","name":"seconds","topic":"","input":"epoch","inputType":"msg","inTz":"America/New_York","adjAmount":0,"adjType":"days","adjDir":"add","format":"ss","locale":"C","output":"seconds","outputType":"msg","outTz":"America/New_York","x":960,"y":860,"wires":[["67b8044e.6bb984"]]},{"id":"766a6b3a.2e952c","type":"inject","z":"150b582b.3e6d68","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":860,"wires":[["38408627.2b4cda"]]},{"id":"67b8044e.6bb984","type":"debug","z":"150b582b.3e6d68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1090,"y":860,"wires":[]}]

see this for formatting it to your needs
https://momentjs.com/docs/#/displaying/format/

I just want de value in milliseconds of the date at midnight as the given by the date picker to match it later with a database.

Can someon help me?

@Rufus - wrote: " I am trying to store some data alongside the date without the hours (in milliseconds).
I want just the date (without the hour) to match the value given by the ui_date_picker."

We are guessing at what you want here because your Question is hard to understand.

The value given by the ui_date_picker is a epoc time in numbers of ms. How can you possibly match the date (without the hour) to a epoc time ????

can you please post the time in the format you want
IE: Jan 18th Feb 2020????

to match it???? it is what ???? take your time ..... write out what your trying to achieve.

Sorry for not being clear. What I am trying to say is that I want a function to give values as the ones given by the date picker.
Example:
Value given by date picker in milliseconds:
Screenshot_2
The same value but in diferent formt to add context:
Screenshot_3

I just want the same value as the first one given by a function.

node-red-contrib-moment does what you need.

[{"id":"6e772f0f.e01a18","type":"moment","z":"150b582b.3e6d68","name":"","topic":"","input":"payload","inputType":"msg","inTz":"America/Detroit","adjAmount":0,"adjType":"days","adjDir":"add","format":"x","locale":"en-US","output":"payload","outputType":"msg","outTz":"America/Detroit","x":780,"y":840,"wires":[["25be28b7.4de34"]]},{"id":"5504e2ae.998644","type":"change","z":"150b582b.3e6d68","name":"msg.payload = 5/3/2021 1:00:00","rules":[{"t":"set","p":"payload","pt":"msg","to":"5/3/2021 1:00:00","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":840,"wires":[["6e772f0f.e01a18"]]},{"id":"5ecb6c8d.9f51d4","type":"debug","z":"150b582b.3e6d68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1130,"y":840,"wires":[]},{"id":"e213fa95.ca5a08","type":"inject","z":"150b582b.3e6d68","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":280,"y":840,"wires":[["5504e2ae.998644"]]},{"id":"25be28b7.4de34","type":"function","z":"150b582b.3e6d68","name":"","func":"//string to number\nmsg.payload = Number(msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":980,"y":840,"wires":[["5ecb6c8d.9f51d4"]]}]

You send it a date and it will send back a time in ms ... epoc
It works both ways.
I recommend reading the documentation on node-red-contrib-moment

Hi @Rufus

I believe I've run into this same issue in the past. What timezone are you accessing the dashboard from? I think you'll find if you change the timezone of the PC accessing the dashboard to GMT/UTC the date picker gives you the correct date.

My workaround was to subtract 2 hours (2x60x60x1000) from the payload (since I'm in UTC+2, and most users will be as well). If a user accesses the dashboard from a time zone UTC+(>2) the datepicker will return the incorrect date.

Edit: I see in a pic you posted that you're in UTC+1. Try subtracting 1x60x60x1000 from the payload. I'm not sure if dst affects you. You might have to adjust accordingly.

Try this...

Essentially, this function can deduct the time offset based on timezone to ensure correct date...

msg.payload = convertUTCDateToLocalDate(msg.payload);

return msg;

function convertUTCDateToLocalDate(date) {
    date = new Date(date);
    var newDate = new Date(date.getTime() + date.getTimezoneOffset() * 60 * 1000);
    var offset = date.getTimezoneOffset() / 60;
    var hours = date.getHours();
    newDate.setHours(hours - offset);
    return newDate;
}

I tried a simple solution (that do the job in my project) and saved the data with a string with the date. I get the date with node-red-contrib-moment node as a sting and split it to get just the date.
Screenshot_5
I´ll try what @Steve-Mcl suggested in the future when I need it.
Thank all for the replies.

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