# Timestamp to current date and time?

**URL:** <https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873>\
**Category:** General\
**Created:** [17 June 2018 15:28 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873 "2018-06-17T15:28:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![flynn](https://avatars.discourse-cdn.com/v4/letter/f/e56c9b/32.png) [@flynn](https://discourse.nodered.org/u/flynn)\
**Post date:** [17 June 2018 15:28 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873/1 "2018-06-17T15:28:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)\
**Post date:** [17 June 2018 15:39 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873/2 "2018-06-17T15:39:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [17 June 2018 15:47 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873/3 "2018-06-17T15:47:31Z")

</div>

Cheat:  
 ![37%20PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/c074225a6aa41f4035ec3693e4c8a4e5aedafcfb.png)

---

<div class="post-metadata">

**Author:** ![flynn](https://avatars.discourse-cdn.com/v4/letter/f/e56c9b/32.png) [@flynn](https://discourse.nodered.org/u/flynn)\
**Post date:** [17 June 2018 16:30 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873/4 "2018-06-17T16:30:17Z")

</div>

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…🙂

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [15 September 2018 03:58 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873/5 "2018-09-15T03:58:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [15 September 2018 09:42 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873/7 "2018-09-15T09:42:03Z")

</div>

> [@craigcurtin](#):
>
> 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

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.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [16 September 2018 00:04 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873/8 "2018-09-16T00:04:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [16 September 2018 00:13 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873/9 "2018-09-16T00:13:49Z")

</div>

> [@craigcurtin](#):
>
> Yep he is brilliant - should give up his day job !!

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 😄

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [16 September 2018 00:16 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873/10 "2018-09-16T00:16:25Z")

</div>

Gotta keep the dream alive !!

Craig

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [16 September 2018 08:07 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873/11 "2018-09-16T08:07:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matts](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@matts](https://discourse.nodered.org/u/matts)\
**Post date:** [4 December 2018 04:47 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873/12 "2018-12-04T04:47:39Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [4 December 2018 10:15 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873/13 "2018-12-04T10:15:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matts](https://avatars.discourse-cdn.com/v4/letter/m/8e8cbc/32.png) [@matts](https://discourse.nodered.org/u/matts)\
**Post date:** [5 December 2018 05:15 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873/14 "2018-12-05T05:15:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dison4linux](https://avatars.discourse-cdn.com/v4/letter/d/b38774/32.png) [@dison4linux](https://discourse.nodered.org/u/dison4linux)\
**Post date:** [11 December 2018 15:45 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873/15 "2018-12-11T15:45:39Z")

</div>

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

 ![50%20AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e98da94af960084527f1f234d029ea131ec9c985.png)

Thanks,  
Jon

---

<div class="post-metadata">

**Author:** ![bwanajag](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bwanajag](https://discourse.nodered.org/u/bwanajag)\
**Post date:** [11 June 2019 12:57 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873/16 "2019-06-11T12:57:44Z")

</div>

Did you get an answer for this? "want\_timestamp\_here"?

---

<div class="post-metadata">

**Author:** ![omriasta](https://avatars.discourse-cdn.com/v4/letter/o/82dd89/32.png) [@omriasta](https://discourse.nodered.org/u/omriasta)\
**Post date:** [25 July 2019 16:45 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873/17 "2019-07-25T16:45:01Z")

</div>

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}}"  
}

---

<div class="post-metadata">

**Author:** ![igrowing](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/igrowing/32/663_2.png) [@igrowing](https://discourse.nodered.org/u/igrowing)\
**Post date:** [26 July 2019 09:04 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873/18 "2019-07-26T09:04:11Z")

</div>

> [@davidcgu](#):
>
> 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.

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](https://github.com/nodejs/node/issues/8500)) is considered as human readable 🙂  
Thank you.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [26 July 2019 09:24 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873/19 "2019-07-26T09:24:08Z")

</div>

@igrowing Have you installed and tried `node-red-contrib-moment`yet (as several people in this thread have suggested)?

---

<div class="post-metadata">

**Author:** ![igrowing](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/igrowing/32/663_2.png) [@igrowing](https://discourse.nodered.org/u/igrowing)\
**Post date:** [26 July 2019 09:29 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873/20 "2019-07-26T09:29:32Z")

</div>

> [@TotallyInformation](#):
>
> To be fair though, Moment.js, while excellent, is BIG. You should avoid it if resources are an issue.

@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.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 July 2019 09:38 UTC](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873/21 "2019-07-26T09:38:38Z")

</div>

> [@igrowing](#):
>
> Is now() function of Node-Red or JS?

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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)  
You would use this in the Function node.

[Next page](https://discourse.nodered.org/t/timestamp-to-current-date-and-time/873.md?page=2)
