Hi all… what would the code be that goes into a function node to take a timestamp and output the date and time in the normal date and time format?
You can write down a function, can take a look for it I have it somewhere in my code however the more easy is use a node called moment, is what I’m using most frequently.
I’m using this on several ways and make a global set of the formatted date, then on any function u can directly recall the global variable and get the date formatted directly as u wish.
Regards
Cheat:
Hey…brilliant guys… thanks. Both those suggestions work. I have gone with the moment node as I managed to get it to display local time - but I did like the json cheat…
Yep Moment.JS is brilliant. The other day i decided to write function to get the date and time into a reasonable format so i could pipe it out inline to a log file with some status info.
After spending 4 hours cruising around (learning a LOT), decided it was a waste of time and far smarter to do it in the moment node - took all of 10 minutes to get it up and running
Craig
I'm amazed that some brilliant chap wrote the moment node to help in situations like this
To be fair though, Moment.js, while excellent, is BIG. You should avoid it if resources are an issue. But it is comprehensive and does cover pretty much every edge-case for date/time handling.
Yep he is brilliant - should give up his day job !!
I run my Node-red on a Ubuntu VM so resources are not really an issue for me
Craig
Haha, sadly his day job pays rather too well to give up right now A few years yet until I can do a "Pete Scargill" and retire
Gotta keep the dream alive !!
Craig
If you want a lightweight solution, try node-red-contrib-simpletime
.
Question..
With this cheat...how can I get the payload/message to also be included in the timestamp?
Example I have a GPIO pin that goes to a change node. It changes a 0 or 1 to a message and then off to an email node. I would like to have it include a timestamp and the message.
Thanks!
payload &" "& $now(), for example.
Thank you so much! I am new to this and trying to teach myself. I understand some complex things..but get tripped on the simple stuff from time to time.
Did you get an answer for this? "want_timestamp_here"?
The easiest way is to use the payload in there. So you can use the timestamp inject node then change that to whatever format you want using a change node. Finally, your json would look like this:
{
"name": "Automated Backup {{payload}}"
}
This simple solution results with string like:
"2019-07-26T09:00:12.214Z"
The string does not fit the current time zone and not quite human readable.
I tried to use $now().toLocaleString(), however now() is not JS function.
How to format $now() to human readable and local timezone format?
The Linux knows the timezone, so I guess Node-Red knows it too.
Any of outputs in the topic above (NodeJS not applying date locales properly · Issue #8500 · nodejs/node · GitHub) is considered as human readable
Thank you.
@igrowing Have you installed and tried node-red-contrib-moment
yet (as several people in this thread have suggested)?
@zenofmud Seems I took this warning too seriously.
Is now() function of Node-Red or JS?
Installed 'simpletime'. Tried to show the datetime in Text UI node as:
{{msg.payload.mydate}} {{msg.payload.mytimes}}
Resulting nothing.
The $now
function that was used in the original reply of this topic is a JSONata function as used in the 'expression' type of the Change node.
JavaScript has its own Date object and all of its associated functions. You can learn about it here: Date - JavaScript | MDN
You would use this in the Function node.