# Timestamp converting

**URL:** <https://discourse.nodered.org/t/timestamp-converting/43461>\
**Category:** General\
**Created:** [31 March 2021 13:41 UTC](https://discourse.nodered.org/t/timestamp-converting/43461 "2021-03-31T13:41:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pecako2001](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pecako2001/32/39552_2.png) [@Pecako2001](https://discourse.nodered.org/u/Pecako2001)\
**Post date:** [31 March 2021 13:41 UTC](https://discourse.nodered.org/t/timestamp-converting/43461/1 "2021-03-31T13:41:19Z")

</div>

Hello,

I'm new to this forum so i dont know where to put this Topic.

I have a problem. I'm trying to publish 2 variables from mqtt with corrensponding date and time into a CSV file.

But the date and time i'm given with the new.Date().toString()  
is in the notation: Wed Mar 31 2021 15:36:06. And i want it in another notation.

Then if i'm using the Date/time Formatter node i transfer this time to the correct time notation : 31.03.2021 15:36.

But how do i get this back into the last string with the variables in it, without overwriting the complete string.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/4/749cf7b13db72be58db1e111a0b11008662f1d90.png)

---

<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:** [31 March 2021 14:44 UTC](https://discourse.nodered.org/t/timestamp-converting/43461/2 "2021-03-31T14:44:53Z")

</div>

> [@Pecako2001](#):
>
> But how do i get this back into the last string with the variables in it, without overwriting the complete string.

I'm not sure I understand this, can you explain it some more?  
Are you saying you want both the `Wed Mar 31 2021 15:36:06` and the `31.03.2021 15:36` format? If so, put them in two different msg variables like msg.longDate and msg.numericDate

---

<div class="post-metadata">

**Author:** ![Pecako2001](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pecako2001/32/39552_2.png) [@Pecako2001](https://discourse.nodered.org/u/Pecako2001)\
**Post date:** [31 March 2021 14:46 UTC](https://discourse.nodered.org/t/timestamp-converting/43461/3 "2021-03-31T14:46:54Z")

</div>

No i'd like to have the `31.03.2021 15:36` format.  
And this has to be in the msg. send to the CSV file

---

<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:** [31 March 2021 14:51 UTC](https://discourse.nodered.org/t/timestamp-converting/43461/4 "2021-03-31T14:51:08Z")

</div>

Why don't you do it in the function node using javascript? Do a google search on 'javascript date conversion'

Is this part of a class assignment?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [31 March 2021 14:55 UTC](https://discourse.nodered.org/t/timestamp-converting/43461/5 "2021-03-31T14:55:05Z")

</div>

Do you mean you want to add a formatted time string alongside the sensor values to be written in your CSV?

What is the msg.payload output from your join node? If it is a key/value object, then simply add a property to msg.payload then it will travel to the CSV node alongside your other values.

If it is something different, share your flow and show us what comes out of the join node and what comes out of the function node.

---

<div class="post-metadata">

**Author:** ![Pecako2001](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pecako2001/32/39552_2.png) [@Pecako2001](https://discourse.nodered.org/u/Pecako2001)\
**Post date:** [31 March 2021 14:59 UTC](https://discourse.nodered.org/t/timestamp-converting/43461/6 "2021-03-31T14:59:42Z")

</div>

I want to have the 2 values (PH, temp) extracted from mqtt in a CSV file  
These values will be extracted from mqtt at the same time cause of the arduino code linked to this.

When these 2 values get published to the CSV file i want the current time and date exported with them.

My code:

```auto
[{"id":"ca5b1816.517648","type":"mqtt in","z":"bde2953.9de2c68","name":"PH waarde","topic":"vijver/ph","qos":"2","datatype":"json","broker":"93299d75.6190a","x":100,"y":620,"wires":[["31c9f087.3438f"]]},{"id":"1200210c.c6833f","type":"mqtt in","z":"bde2953.9de2c68","name":"Temp waarde","topic":"vijver/temp","qos":"2","datatype":"json","broker":"93299d75.6190a","x":110,"y":680,"wires":[["7b8e0037.f117a"]]},{"id":"15ba4b3a.fa8815","type":"file","z":"bde2953.9de2c68","name":"","filename":"/share/test.csv","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"none","x":960,"y":640,"wires":[[]]},{"id":"ce878c18.15589","type":"join","z":"bde2953.9de2c68","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":450,"y":640,"wires":[["b52021a0.03338"]]},{"id":"31c9f087.3438f","type":"change","z":"bde2953.9de2c68","name":"","rules":[{"t":"set","p":"parts","pt":"msg","to":"2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":620,"wires":[["ce878c18.15589"]]},{"id":"7b8e0037.f117a","type":"change","z":"bde2953.9de2c68","name":"","rules":[{"t":"set","p":"parts","pt":"msg","to":"3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":680,"wires":[["ce878c18.15589"]]},{"id":"b52021a0.03338","type":"function","z":"bde2953.9de2c68","name":"Name of database + timestamp","func":"msg.filename = \"/share/test.txt\";\nvar time = new Date().toString().replace(\" GMT+0200 (Central European Summer Time)\", \"\");\nvar x = msg.payload[0];\nvar y = msg.payload[1];\nmsg.payload = time + ';' + x + ';PH' + ';' + y + \";Graden\" ;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":690,"y":640,"wires":[["15ba4b3a.fa8815","55ae35c3.d6d0ac"]]},{"id":"27d23756.1fe1c8","type":"function","z":"bde2953.9de2c68","name":"Name of database + timestamp","func":"var time = new Date().toString().replace(\"Wed\", \"\").replace(\" GMT+0200 (Central European Summer Time)\", \"\");\nmsg.payload = time;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":490,"y":760,"wires":[["fc6a4f83.2ede8"]]},{"id":"fc6a4f83.2ede8","type":"date-converter","z":"bde2953.9de2c68","name":"","topic":"","input":"payload","inputType":"msg","inTz":"","adjAmount":0,"adjType":"days","adjDir":"add","format":"DD.MM.YYYY HH:mm","locale":"","output":"payload","outputType":"msg","outTz":"","x":760,"y":760,"wires":[["55ae35c3.d6d0ac"],[]]},{"id":"b74db90d.f223a8","type":"inject","z":"bde2953.9de2c68","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":230,"y":760,"wires":[["27d23756.1fe1c8"]]},{"id":"55ae35c3.d6d0ac","type":"debug","z":"bde2953.9de2c68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1000,"y":720,"wires":[]},{"id":"93299d75.6190a","type":"mqtt-broker","name":"","broker":"192.168.1.173","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [31 March 2021 18:16 UTC](https://discourse.nodered.org/t/timestamp-converting/43461/7 "2021-03-31T18:16:56Z")

</div>

Try formating the date with moments using JSONata expression in a change node. Also you are joining with an array, depending on which message is received first your PH and Temp may be reversed. Try joining as an object  
e.g.

```auto
[{"id":"ce878c18.15589","type":"join","z":"5a245aa1.510164","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"parts","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":420,"y":3520,"wires":[["26df3b60.9edf54"]]},{"id":"31c9f087.3438f","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"parts","pt":"msg","to":"ph","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":250,"y":3500,"wires":[["ce878c18.15589"]]},{"id":"7b8e0037.f117a","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"parts","pt":"msg","to":"temp","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":250,"y":3560,"wires":[["ce878c18.15589"]]},{"id":"26df3b60.9edf54","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"payload.time","pt":"msg","to":"$moment().tz(\"Europe/london\").format(\"DD.MM.YYYY HH:mm\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":3580,"wires":[["b52021a0.03338"]]},{"id":"6904e44e.8fbc84","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"20","payloadType":"num","x":90,"y":3500,"wires":[["31c9f087.3438f"]]},{"id":"8dda8b50.f07a8","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"7","payloadType":"num","x":90,"y":3560,"wires":[["7b8e0037.f117a"]]},{"id":"b52021a0.03338","type":"function","z":"5a245aa1.510164","name":"Name of database + timestamp","func":"msg.filename = \"/share/test.txt\";\nvar time = msg.payload.time;\nvar y = msg.payload.ph\nvar x = msg.payload.temp;\nmsg.payload = time + ';' + x + ';PH' + ';' + y + \";Graden\" ;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":840,"y":3540,"wires":[["55ae35c3.d6d0ac"]]},{"id":"55ae35c3.d6d0ac","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":970,"y":3600,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Pecako2001](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pecako2001/32/39552_2.png) [@Pecako2001](https://discourse.nodered.org/u/Pecako2001)\
**Post date:** [1 April 2021 15:19 UTC](https://discourse.nodered.org/t/timestamp-converting/43461/8 "2021-04-01T15:19:51Z")

</div>

Thank you, this solution works great!!

---

<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:** [15 April 2021 15:19 UTC](https://discourse.nodered.org/t/timestamp-converting/43461/9 "2021-04-15T15:19:52Z")

</div>

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