# Add a Timestamp to Function node

**URL:** <https://discourse.nodered.org/t/add-a-timestamp-to-function-node/22610>\
**Category:** General\
**Created:** [4 March 2020 11:20 UTC](https://discourse.nodered.org/t/add-a-timestamp-to-function-node/22610 "2020-03-04T11:20:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Job-Hoogeboom](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/job-hoogeboom/32/19629_2.png) [@Job-Hoogeboom](https://discourse.nodered.org/u/Job-Hoogeboom)\
**Post date:** [4 March 2020 11:20 UTC](https://discourse.nodered.org/t/add-a-timestamp-to-function-node/22610/1 "2020-03-04T11:20:31Z")

</div>

Hi!

I'm using the following flow to read a Modbus power meter [Flow 2](https://ibb.co/mBzC8Td), this is the string in the function node [Function node](https://ibb.co/GPww6f3). I would like to add a time format to the msg.payload.

something like this

{"Time": 2020-03-04 12:15:10}  
{"Voltage":228.16}

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 March 2020 11:37 UTC](https://discourse.nodered.org/t/add-a-timestamp-to-function-node/22610/2 "2020-03-04T11:37:46Z")

</div>

> [@Job-Hoogeboom](#):
>
> {"Time": 2020-03-04 12:15:10}  
> {"Voltage":228.16}

Do you mean `{"Time": 2020-03-04 12:15:10, "Voltage":228.16}` in the payload?  
Can I first ask why you want the time as a string rather than a javascript timestamp? Often it is best to keep it as a timestamp and only convert to string when needed for display.

---

<div class="post-metadata">

**Author:** ![Job-Hoogeboom](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/job-hoogeboom/32/19629_2.png) [@Job-Hoogeboom](https://discourse.nodered.org/u/Job-Hoogeboom)\
**Post date:** [4 March 2020 12:20 UTC](https://discourse.nodered.org/t/add-a-timestamp-to-function-node/22610/3 "2020-03-04T12:20:39Z")

</div>

Yes that would work too, I'm still learning in node-red. I need the timestamp so i can visualize it in AWS-cloud analytics.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 March 2020 12:26 UTC](https://discourse.nodered.org/t/add-a-timestamp-to-function-node/22610/4 "2020-03-04T12:26:41Z")

</div>

That is very easy then, if you pass the message containing a payload of `{"Voltage":228.16}` to a function node containing

```auto
msg.payload.Time = new Date()
return msg;

```

then that will do it. That assumes you already have the voltage property already setup. If you just have that value as a number in the payload then you can put them both in using

```auto
msg.payload = {Time: new Date(), Voltage: msg.payload}
return msg;

```

---

<div class="post-metadata">

**Author:** ![Job-Hoogeboom](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/job-hoogeboom/32/19629_2.png) [@Job-Hoogeboom](https://discourse.nodered.org/u/Job-Hoogeboom)\
**Post date:** [4 March 2020 12:44 UTC](https://discourse.nodered.org/t/add-a-timestamp-to-function-node/22610/5 "2020-03-04T12:44:19Z")

</div>

Thanks for the help Colin! it worked for me!

---

<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:** [4 March 2020 15:14 UTC](https://discourse.nodered.org/t/add-a-timestamp-to-function-node/22610/6 "2020-03-04T15:14:06Z")

</div>

Personally, I would output the timestamp in an ISO format because that is easily processed and cannot be misinterpreted but is still human readable.

```auto
msg.payload.Time = (new Date()).toISOString()
return msg;

```

Output looks like: `2020-03-04T15:13:21.294Z`

Note that ISO timestamps are always in Zulu timezone (UTC).

---

<div class="post-metadata">

**Author:** ![unixneo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/unixneo/32/17441_2.png) [@unixneo](https://discourse.nodered.org/u/unixneo)\
**Post date:** [4 March 2020 16:08 UTC](https://discourse.nodered.org/t/add-a-timestamp-to-function-node/22610/7 "2020-03-04T16:08:57Z")

</div>

FYI:

Either way,.... this will often not work and give errors:

```auto
[
{"Time": 2020-03-04 12:15:10},
{"Voltage":228.16}
]

```

The time string needs to be a string, to be sure:

```auto
{"Time": "2020-03-04 12:15:10"}

```

so this will also will have problems, for the same reason:

```auto
{"Time": 2020-03-04 12:15:10, "Voltage":228.16}

```

Should be formatted in JSON as a string, to insure smooth sailing:

```auto
{"Time": "2020-03-04 12:15:10", "Voltage":228.16}

```

Sometimes when having problems (or just building JSON strings), it is a good idea to simply and quickly take your proposed JSON string and copy-and-paste it into some kind of visual editor which will format the JSON.

For example, I use Visual Studio Code, and when I have a JSON string when developing / programming, I quickly copy-and-paste the string into a VSC file with a `.json` extension and it either formats as a proper JSON string or it does not.

This will often save (us) a lot of time trying to get an API or JSON payload to work when there is some JSON formatting error.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 March 2020 16:27 UTC](https://discourse.nodered.org/t/add-a-timestamp-to-function-node/22610/8 "2020-03-04T16:27:03Z")

</div>

> [@unixneo](#):
>
> The time string needs to be a string, to be sure:

How do you know? Does it not depend on where you are sending it? Sending a javascript date may be perfectly valid.  
In addition if sending as a string it is often important to include the timezone information.  
`{Time: new Date()}`  
does that implicitly.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [18 March 2020 16:27 UTC](https://discourse.nodered.org/t/add-a-timestamp-to-function-node/22610/9 "2020-03-18T16:27:09Z")

</div>

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