# Add date to 'save to file' node

**URL:** <https://discourse.nodered.org/t/add-date-to-save-to-file-node/993>\
**Category:** FAQs\
**Created:** [21 June 2018 18:29 UTC](https://discourse.nodered.org/t/add-date-to-save-to-file-node/993 "2018-06-21T18:29:11Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ardv](https://avatars.discourse-cdn.com/v4/letter/a/58956e/32.png) [@ardv](https://discourse.nodered.org/u/ardv)\
**Post date:** [21 June 2018 18:29 UTC](https://discourse.nodered.org/t/add-date-to-save-to-file-node/993/1 "2018-06-21T18:29:11Z")

</div>

Hello,  
i want that the ‘save to file’ node uses the current date in the filename, so i added function in which i add ‘date’ property to the msg, and then in the save node i wrote: “{{msg.date}}.csv”, but the node is not reading the date property and it stores the file with this name: “{{msg.date}}.csv”.  
i hope that you can help.

---

<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:** [21 June 2018 18:37 UTC](https://discourse.nodered.org/t/add-date-to-save-to-file-node/993/2 "2018-06-21T18:37:29Z")

</div>

You cannot use the `{{ }}` syntax in the file name field. You need to generate the filename you want and pass it into the node as `msg.filename` - as described in the info sidebar of the File node.

---

<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:** [21 June 2018 18:38 UTC](https://discourse.nodered.org/t/add-date-to-save-to-file-node/993/3 "2018-06-21T18:38:01Z")

</div>

It would be helpful if you provided the code you used in the function…

---

<div class="post-metadata">

**Author:** ![ardv](https://avatars.discourse-cdn.com/v4/letter/a/58956e/32.png) [@ardv](https://discourse.nodered.org/u/ardv)\
**Post date:** [21 June 2018 20:30 UTC](https://discourse.nodered.org/t/add-date-to-save-to-file-node/993/4 "2018-06-21T20:30:05Z")

</div>

Thank you.  
that’s the code i wrote in the function:

```auto
var d = new Date();
var t = d.getTime();
var year = d.getFullYear();
var month = d.getMonth()+1; 
if(month.toString().length == 1) {
var month = '0'+month;
}
var day = d.getDate();
var hour = d.getHours();
msg.date = t;
msg.filename = "measurements/"+year+month+day+".csv";
return msg;

```

and it is working well.

---

<div class="post-metadata">

**Author:** ![Sotiris\_Sy](https://avatars.discourse-cdn.com/v4/letter/s/ba9def/32.png) [@Sotiris\_Sy](https://discourse.nodered.org/u/Sotiris_Sy)\
**Post date:** [8 June 2020 09:46 UTC](https://discourse.nodered.org/t/add-date-to-save-to-file-node/993/5 "2020-06-08T09:46:29Z")

</div>

Hi all

Can you please provide the code with the node

Thanks

---

<div class="post-metadata">

**Author:** ![Tokn59](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tokn59/32/35325_2.png) [@Tokn59](https://discourse.nodered.org/u/Tokn59)\
**Post date:** [13 January 2021 17:29 UTC](https://discourse.nodered.org/t/add-date-to-save-to-file-node/993/6 "2021-01-13T17:29:05Z")

</div>

Did you manage to find the right syntax for the node code ?

---

<div class="post-metadata">

**Author:** ![Merwenus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/merwenus/32/41604_2.png) [@Merwenus](https://discourse.nodered.org/u/Merwenus)\
**Post date:** [30 August 2023 17:09 UTC](https://discourse.nodered.org/t/add-date-to-save-to-file-node/993/7 "2023-08-30T17:09:37Z")

</div>

it shows error, does not work.

---

<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:** [31 August 2023 07:55 UTC](https://discourse.nodered.org/t/add-date-to-save-to-file-node/993/8 "2023-08-31T07:55:22Z")

</div>

Show us the error.

---

<div class="post-metadata">

**Author:** ![Merwenus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/merwenus/32/41604_2.png) [@Merwenus](https://discourse.nodered.org/u/Merwenus)\
**Post date:** [31 August 2023 08:34 UTC](https://discourse.nodered.org/t/add-date-to-save-to-file-node/993/9 "2023-08-31T08:34:52Z")

</div>

It just says error in javascript code on this line:  
var month = '0' + month;

---

<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:** [31 August 2023 08:42 UTC](https://discourse.nodered.org/t/add-date-to-save-to-file-node/993/10 "2023-08-31T08:42:19Z")

</div>

Please export the function node that is showing the error and paste it here. Use the `</>` button at the top of the forum entry window when pasting it in, to prevent the forum messing with the format.

Also stop node red and start it again in a command window and paste the startup log here. Use the `</>` button again when pasting that.

---

<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 August 2023 10:46 UTC](https://discourse.nodered.org/t/add-date-to-save-to-file-node/993/11 "2023-08-31T10:46:49Z")

</div>

> [@Merwenus](#):
>
> says error in javascript code on this line:  
> var month = '0' + month;

It is not an error - more of a "there is something not right".

The problem is the code you have used is "not good". It is declaring `var month` twice - that is the warning.

Here is an updated version:

```auto
const d = new Date()
const year = d.getFullYear()
const month = (d.getMonth() + 1).toString().padStart(2, '0')
const day = d.getDate().toString().padStart(2, '0')
msg.date = d.getTime()
msg.filename = `measurements/${year}-${month}-${day}.csv`
return msg

```

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

---

<div class="post-metadata">

**Author:** ![Merwenus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/merwenus/32/41604_2.png) [@Merwenus](https://discourse.nodered.org/u/Merwenus)\
**Post date:** [31 August 2023 11:19 UTC](https://discourse.nodered.org/t/add-date-to-save-to-file-node/993/12 "2023-08-31T11:19:01Z")

</div>

> [@Steve-Mcl](#):
>
> `msg.date `

Thanks, no error message but it still does not save the file name and use it as an external link. :  
What am I missing here?  
Plan was originally when someone presses Doorbell, it takes a snapshot and send a rich notification to HA with an external link to the jpg. But internet decided to cache the jpg file so I get the same image over and over. Second plan was to use a timestamp that saves every visitor (better plan), but can't find a solution that works with filename and external link. ☹ I get doorbell\_.jpg nonstop

```auto
[{"id":"934a2575fef20641","type":"inject","z":"b0334f5a6d891812","name":"Start","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":990,"wires":[["fa0b37c43d26efd1"]]},{"id":"18610cc8d82ca6da","type":"api-call-service","z":"b0334f5a6d891812","name":"Doorbell Snapshot","server":"f6a86a23.b6858","version":1,"debugenabled":false,"service_domain":"camera","service":"snapshot","entityId":"camera.doorbell_sub","data":"{\"filename\":\"/config/www/doorbell_{{ msg.date }}.jpg\"}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":480,"y":990,"wires":[["9db16d0408606e97"]]},{"id":"fa0b37c43d26efd1","type":"function","z":"b0334f5a6d891812","name":"Get Date","func":"const d = new Date()\nconst year = d.getFullYear()\nconst month = (d.getMonth() + 1).toString().padStart(2, '0')\nconst day = d.getDate().toString().padStart(2, '0')\nmsg.date = d.getTime()\nmsg.filename = `measurements/${year}-${month}-${day}.csv`\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":990,"wires":[["18610cc8d82ca6da"]]},{"id":"9db16d0408606e97","type":"function","z":"b0334f5a6d891812","name":"External Notification","func":"msg.payload = {\n data: {\n \"message\":\"Csengettek !\",\n \"data\":\n {\n \"image\": \"https://internet.com/local/doorbell_{{ msg.date }}.jpg\",\n \"channel\":\"Front Doorbell\",\n \"priority\":\"high\",\n \"ttl\": 0,\n \"actions\":\n [{\n \"action\": \"URI\",\n \"title\": \"Kamera megnyitás\",\n \"uri\": \"app://com.mcu.reolink\"\n }]\n }\n }\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":990,"wires":[["86f6cad23ef87d09"]]},{"id":"86f6cad23ef87d09","type":"api-call-service","z":"b0334f5a6d891812","name":"Phone notification","server":"f6a86a23.b6858","version":1,"debugenabled":true,"service_domain":"notify","service":"mobile_app_m2102k1g","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":920,"y":990,"wires":[[]]},{"id":"f6a86a23.b6858","type":"server","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

```

---

<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 August 2023 11:45 UTC](https://discourse.nodered.org/t/add-date-to-save-to-file-node/993/13 "2023-08-31T11:45:01Z")

</div>

Since you are using HA - I have no idea. AFAIK, most folk here prefer to do everything in node-red.

This is what I (and most other on this forum) see when i import your flow:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/2/32eac0d13a669530b6ec5a0b17024688aab36502.png)

As you can see, I have no idea what those nodes are and nor do i known settings they have

* * *

NOTE: The original intention of this thread (that is, BTW, over 5 years old), was to generate a dynamic filename in `msg.filename` - probably for sending to the `file` node. As you can see from my screenshot does just that.

Just because the `msg` now has a `filename` property does NOT mean poking that into a HA node will somehow change a filename somewhere in the bowels of HA.

My only suggestion at this point would be start a new thread and be explicit with what you are doing and that you are using HA etc - OR (probably better) - head over to the HA forum where you can get HA advise on the HA parts of your flow.
