# Filename with dynamic date in node red

**URL:** <https://discourse.nodered.org/t/filename-with-dynamic-date-in-node-red/15556>\
**Category:** General\
**Created:** [15 September 2019 12:30 UTC](https://discourse.nodered.org/t/filename-with-dynamic-date-in-node-red/15556 "2019-09-15T12:30:06Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![vks007](https://avatars.discourse-cdn.com/v4/letter/v/6bbea6/32.png) [@vks007](https://discourse.nodered.org/u/vks007)\
**Post date:** [15 September 2019 12:30 UTC](https://discourse.nodered.org/t/filename-with-dynamic-date-in-node-red/15556/1 "2019-09-15T12:30:06Z")

</div>

I am calling the camera snapshot service in nodered and specifying the following as Data :

`{ "filename": "/config/www/bedroom{{ now().strftime(\"%Y%m%d-%H%M%S\") }}.jpg" }`

This works fine if I call the service directly from HA and it creates a file with timestamp but from node red it doesnt take the timestamp into account and creates bedroom.jpg as the file name. what am I missing?

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [15 September 2019 12:33 UTC](https://discourse.nodered.org/t/filename-with-dynamic-date-in-node-red/15556/2 "2019-09-15T12:33:26Z")

</div>

> [@vks007](#):
>
> `{{ now().strftime(\"%Y%m%d-%H%M%S\") }}`

This part of the syntax is python specific, it evaluates the current date time into a yyyy-mm-dd-hhmmss syntax. So you’ll need to replace that part by something that works in JavaScript/Node-RED. The double braces syntax here is specifically to the Jinja2 library in Python. Note that this might execute in JavaScript as Mustache, however the syntax that follows is meant for Python/Jinja2.

---

<div class="post-metadata">

**Author:** ![vks007](https://avatars.discourse-cdn.com/v4/letter/v/6bbea6/32.png) [@vks007](https://discourse.nodered.org/u/vks007)\
**Post date:** [15 September 2019 12:36 UTC](https://discourse.nodered.org/t/filename-with-dynamic-date-in-node-red/15556/3 "2019-09-15T12:36:57Z")

</div>

ah! of course, thanks for pointing that out. I'll find a way to do it in javascript.

---

<div class="post-metadata">

**Author:** ![vks007](https://avatars.discourse-cdn.com/v4/letter/v/6bbea6/32.png) [@vks007](https://discourse.nodered.org/u/vks007)\
**Post date:** [15 September 2019 12:47 UTC](https://discourse.nodered.org/t/filename-with-dynamic-date-in-node-red/15556/4 "2019-09-15T12:47:43Z")

</div>

@afelix Looking around it seems it isnt straight forward to specify a datetime within json , can you give me some pointers please.  
I could do a new date , format it and return if it was a function but not sure how to do it within a json string

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [15 September 2019 13:03 UTC](https://discourse.nodered.org/t/filename-with-dynamic-date-in-node-red/15556/5 "2019-09-15T13:03:48Z")

</div>

You’re not doing it in a json string.

Your doing it in a javascript object.  
If you format the date as a veritable you can then easily append that using the syntax

msg.whatever= “stuff”+variable+”otherstuff”

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [15 September 2019 13:21 UTC](https://discourse.nodered.org/t/filename-with-dynamic-date-in-node-red/15556/6 "2019-09-15T13:21:06Z")

</div>

```auto
var now = new Date();
// Create formatted time
var yyyy = now.getFullYear();
var mm = now.getMonth() < 9 ? "0" + (now.getMonth() + 1) : (now.getMonth() + 1); // getMonth() is zero-based
var dd = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
var hh = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
var mmm = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
var ss = now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds();

// file path with / at the end
var path = "/home/pi/node-red-static/"; // This is the path
var filename = "cam3_"+yyyy+mm+dd+"-"+hh+mm+ss+".jpg"; // file name

```

This one works for me

---

<div class="post-metadata">

**Author:** ![vks007](https://avatars.discourse-cdn.com/v4/letter/v/6bbea6/32.png) [@vks007](https://discourse.nodered.org/u/vks007)\
**Post date:** [15 September 2019 18:03 UTC](https://discourse.nodered.org/t/filename-with-dynamic-date-in-node-red/15556/7 "2019-09-15T18:03:21Z")

</div>

Thanks @edje11 , I was hoping to put something within the Data field of the call service node but then I can always include a function node before that and pass on the filename as an input to the call service node. Thanks

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [18 September 2019 18:19 UTC](https://discourse.nodered.org/t/filename-with-dynamic-date-in-node-red/15556/8 "2019-09-18T18:19:58Z")

</div>

FYI, here is another thread with a solution that requires _no programming_...

> [@Msg.filename logging data](https://discourse.nodered.org/t/msg-filename-logging-data/1117/13):
>
> In the spirit of "there's always another way to do it", I like to use the node-red-contrib-moment node whenever I need to format time and date strings. Here is an example of how to append the current timestamp formatted as YYYYMMDD to a static filename: [image] Just put whatever static strings you want inside [...] square brackets, set the input to be the current timestamp, and use any of [moment's many formatting directives](http://momentjs.com/docs/#/displaying/format/). In your case, you would also set the Output to msg. filename -- no …

---

<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 16:33 UTC](https://discourse.nodered.org/t/filename-with-dynamic-date-in-node-red/15556/9 "2021-01-13T16:33:02Z")

</div>

This is great but I still have a question. After constructing this file name I need to use the Cal, Service Snapshot node. How do I pass on the path and filename in my filename definition in this node ?

So the standard is :

{  
"filename": "/config/www/somefilename.jpg"  
}

How can I modify this and use var path and filename here ?

Many Thanks !!!

---

<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:** [13 January 2021 18:55 UTC](https://discourse.nodered.org/t/filename-with-dynamic-date-in-node-red/15556/10 "2021-01-13T18:55:05Z")

</div>

@Tokn59 Plese don't post the same thing in multiple threads. I will close this thread since it is over a year old.

---

<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:** [13 January 2021 18:55 UTC](https://discourse.nodered.org/t/filename-with-dynamic-date-in-node-red/15556/11 "2021-01-13T18:55:12Z")

</div>


