Timestamp to current date and time?


#1

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?


#2

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


#3

Cheat:
37%20PM


#4

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…:slight_smile:


#5

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


#7

I'm amazed that some brilliant chap wrote the moment node to help in situations like this :wink:

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.


#8

Yep he is brilliant - should give up his day job !! :grinning::rofl:

I run my Node-red on a Ubuntu VM so resources are not really an issue for me

Craig


#9

Haha, sadly his day job pays rather too well to give up right now :wink: A few years yet until I can do a "Pete Scargill" and retire :smile:


#10

Gotta keep the dream alive !!

Craig


#11

If you want a lightweight solution, try node-red-contrib-simpletime.


#12

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!


#13

payload &" "& $now(), for example.


#14

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.


#15

How can you include a timestamp into existing JSON data used by HomeAssistant?

Thanks,
Jon