Getting Today's Date

I am trying to get the days date whenever a flow is run and store that date in slots.date.

I tried this, the code was modified from a get time node.

var dateString = new Date().toLocaleTimeString([],
{
    day: "2-digit", 
    month: "2-digit"
})

{
msg.slots.date = dateString;
}
return msg;

I get the current date but it also includes the time, which I do not want. I would like to change the format of the date from the 11/30 to Tues Nov 30.
I looked at some JS docs but everything I found was using Date() to set the date not retrieve it.

They can't have been comprehensive docs then. Look for the get... functions here Date - JavaScript | MDN

An alternative using a Change node with a Jsonata $moment() expression

[{"id":"57ce8155a9b3ed3a","type":"change","z":"54efb553244c241f","name":"","rules":[{"t":"set","p":"slots.date","pt":"msg","to":"$moment().format('ddd MMM DD')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":900,"wires":[["e952d58798dbf0d3"]]},{"id":"7efd9ae22d4311e5","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":350,"y":900,"wires":[["57ce8155a9b3ed3a"]]},{"id":"e952d58798dbf0d3","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":740,"y":900,"wires":[]}]

image

Or return the Date() as a string and split it to return the first 3 elements, then rejoin them. Or as Unborn says use JSONata and moments

[{"id":"f6a7dd1b.111578","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"accX\":-0.123456,\"accY\":1.123456,\"accZ\":4.123456}","payloadType":"json","x":90,"y":500,"wires":[["9526971f.125058","1ba7310.f637acf"]]},{"id":"9526971f.125058","type":"function","z":"c791cbc0.84f648","name":"","func":"var dateString = new Date().toString().split(\" \", 3).join(\" \");\nmsg.slots= {date: dateString}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":420,"wires":[["f3f50673.aac85"]]},{"id":"1ba7310.f637acf","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"slots.date","pt":"msg","to":"$moment().tz(\"euroupe/London\").format(\"ddd MMM Do\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":540,"wires":[["f3f50673.aac85"]]},{"id":"f3f50673.aac85","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"slots.date","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":420,"wires":[]}]

[edit] I added how to set localtime and the st, nd and th to the day of the month.
https://momentjs.com/docs/#/displaying/format/

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