# Add local date and time to a CSV-file

**URL:** <https://discourse.nodered.org/t/add-local-date-and-time-to-a-csv-file/45658>\
**Category:** General\
**Created:** [14 May 2021 15:03 UTC](https://discourse.nodered.org/t/add-local-date-and-time-to-a-csv-file/45658 "2021-05-14T15:03:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![HaVi](https://avatars.discourse-cdn.com/v4/letter/h/b3f665/32.png) [@HaVi](https://discourse.nodered.org/u/HaVi)\
**Post date:** [14 May 2021 15:03 UTC](https://discourse.nodered.org/t/add-local-date-and-time-to-a-csv-file/45658/1 "2021-05-14T15:03:28Z")

</div>

My intention is to convert a measurement "luchtdruk" (pressure via MQTT) together with local date and time into a comma separated CSV file: date, time, pressure.

As you can see in the graphic I use the Moment Node with output format _DD-MM-YYYY, HH: mm_ to generate the local date and time. The output of the Change Node shows the correct date, time and pressure. But the CSV node only outputs the pressure, so no date and no time.

I've been figuring it out for a few hours but I can't. Can anyone advise me?

 ![forum](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/b/0b20412df0fc2ede03feb6e210d38d3ada64c34a.png)

```auto
[{"id":"c01f62b6.2cf2c","type":"tab","label":"LoRa Divers","disabled":false,"info":""},{"id":"f97042a6.651e9","type":"mqtt in","z":"c01f62b6.2cf2c","name":"Luchtdruk","topic":"v3/82671@ttn/devices/98732/up","qos":"2","datatype":"json","broker":"47f2cc62.a875c4","nl":false,"rap":false,"x":80,"y":60,"wires":[["a5a107a.ab9bbf8"]]},{"id":"f492c296.fe8f9","type":"csv","z":"c01f62b6.2cf2c","name":"CSV","sep":",","hdrin":false,"hdrout":"none","multi":"one","ret":"\\n","temp":"datetime,luchtdruk","skip":"0","strings":true,"include_empty_strings":true,"include_null_values":false,"x":550,"y":60,"wires":[[]]},{"id":"33d079ae.9bb146","type":"change","z":"c01f62b6.2cf2c","name":"Change","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t\"datumtijd\": payload.datetime,\t\"luchtdruk\": payload.uplink_message.decoded_payload.luchtdruk\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":60,"wires":[["f492c296.fe8f9"]]},{"id":"a5a107a.ab9bbf8","type":"moment","z":"c01f62b6.2cf2c","name":"DateTime","topic":"","input":"payload.uplink_message.received_at","inputType":"msg","inTz":"Europe/Amsterdam","adjAmount":0,"adjType":"days","adjDir":"add","format":"DD-MM-YYYY,HH:mm","locale":"nl-NL","output":"payload.datetime","outputType":"msg","outTz":"Europe/Amsterdam","x":240,"y":60,"wires":[["33d079ae.9bb146"]]},{"id":"47f2cc62.a875c4","type":"mqtt-broker","name":"Luchtdruk","broker":"eu1.cloud.thethings.network","port":"1883","clientid":"","usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

```

---

<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:** [14 May 2021 15:22 UTC](https://discourse.nodered.org/t/add-local-date-and-time-to-a-csv-file/45658/2 "2021-05-14T15:22:01Z")

</div>

The column names in the CSV must match the names in the object.

You have "datetime" as one of the columns in the CSV but it is not called that in the object that comes from the change node.

---

<div class="post-metadata">

**Author:** ![HaVi](https://avatars.discourse-cdn.com/v4/letter/h/b3f665/32.png) [@HaVi](https://discourse.nodered.org/u/HaVi)\
**Post date:** [14 May 2021 16:36 UTC](https://discourse.nodered.org/t/add-local-date-and-time-to-a-csv-file/45658/3 "2021-05-14T16:36:33Z")

</div>

I assumed it didn't matter what names I gave those columns, but it does. Thank you for taking a look, your help is greatly appreciated!

Now I run into another problem, the date and time are in quotation marks (image), perhaps because it is a string? Do you know how I can fix that?

![datetime](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/a/5a60170ef233663da2b69e2505bd6013b1b2d218.png)

---

<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:** [14 May 2021 16:38 UTC](https://discourse.nodered.org/t/add-local-date-and-time-to-a-csv-file/45658/4 "2021-05-14T16:38:52Z")

</div>

The date is in quotes because there is a comma in the timestamp.

Your options are to change the separator to another character (like tab) OR change the format of the timestamp so that it does not include a comma.

---

<div class="post-metadata">

**Author:** ![HaVi](https://avatars.discourse-cdn.com/v4/letter/h/b3f665/32.png) [@HaVi](https://discourse.nodered.org/u/HaVi)\
**Post date:** [14 May 2021 16:43 UTC](https://discourse.nodered.org/t/add-local-date-and-time-to-a-csv-file/45658/5 "2021-05-14T16:43:44Z")

</div>

In that case I choose the tab. Thanks again for your help, nice to get a response so quickly.

Today I am still a beginner but a little less beginner than yesterday 😉

---

<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:** [14 May 2021 20:30 UTC](https://discourse.nodered.org/t/add-local-date-and-time-to-a-csv-file/45658/6 "2021-05-14T20:30:07Z")

</div>

If the timestamp column is to be used by a computer for something (rather than being read by a person) then it is usually best to use an iso string format. To get that, in a function node, you can use `let now = new Date().toISOString()`. That will give you a string like `2021-05-05T14:48:00.000Z`  
You don't need the moment node in that case.

---

<div class="post-metadata">

**Author:** ![HaVi](https://avatars.discourse-cdn.com/v4/letter/h/b3f665/32.png) [@HaVi](https://discourse.nodered.org/u/HaVi)\
**Post date:** [15 May 2021 07:25 UTC](https://discourse.nodered.org/t/add-local-date-and-time-to-a-csv-file/45658/7 "2021-05-15T07:25:29Z")

</div>

You are right Colin, but I use this method because the CSV-file is processed by a Wordpress plugin on a web server. The plugin cannot work properly with the usual timestamps such as Unix.

An additional advantage of the Moment node is that it fetches the local time (with DTS) correctly.

---

<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:** [29 May 2021 07:25 UTC](https://discourse.nodered.org/t/add-local-date-and-time-to-a-csv-file/45658/8 "2021-05-29T07:25:37Z")

</div>

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