Node Red filename-timestamp

Standard timestamps can't be used to build filenames, but sometimes it is useful to have a time indication in a unique file name (logs, data, etc.) This node creates timestamps to be used in filenames, dirs, DB table names, etc. like "D20200310T115905".

[{"id":"2861ce58.aff572","type":"function","z":"a3d794f8.c43c28","name":"filename timestamp","func":"\nDate.prototype.pad2 =      function(n) {  \n  return (n < 10 ? '0' : '') + n;\n}\nDate.prototype.yyyymmdd = function() {\n  return  this.getFullYear()+this.pad2(this.getMonth() + 1)+this.pad2(this.getDate());\n};\n\nDate.prototype.yyyymmddThhmmss = function() {\n  return \"D\"+this.yyyymmdd()+\"T\"+ this.pad2(this.getHours())+ this.pad2(this.getMinutes())+ this.pad2(this.getSeconds());\n};\n\nmsg.payload = new Date().yyyymmddThhmmss();\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":860,"wires":[["2a143ec.06dfdc2"]],"info":"Standard timestamps can't be used to build filenames, but sometimes it is useful to have a time indication in a unique file name (logs, data, etc.)\nThis node creates timestamps to be used in filenames.\n### How it works\nThis node adds 3 new functions to Date()\n * pad2(n) returns like \"03\"\n * yyyymmdd() returns like \"20200310\"\n * yyyymmddThhmmss() returns like \"D20200310T104655\"\n\n### How use it\n**input:** a trigger msg (any)\n\n**output:** msg.payload = timestamp by yyyymmddThhmmss()\n"},{"id":"1969d854.5b72e8","type":"inject","z":"a3d794f8.c43c28","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":860,"wires":[["2861ce58.aff572"]]},{"id":"2a143ec.06dfdc2","type":"debug","z":"a3d794f8.c43c28","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":860,"wires":[]}]

Hope this can help you.
Best regards
m.s.

You can also use node-red-contrib-moment to format a timestamp into a string. The main benefit is the format is entered as a string with the standard tokens.

For example, the format string \DYYYYMMDD\THHmmss converts 1583841968068 into D20200310T130608 in the following flow.

[{"id":"4209ab99.c895c4","type":"inject","z":"2bc4cb78.899144","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":260,"wires":[["68ddce51.832f"]]},{"id":"68ddce51.832f","type":"moment","z":"2bc4cb78.899144","name":"","topic":"","input":"","inputType":"msg","inTz":"Europe/Paris","adjAmount":0,"adjType":"days","adjDir":"add","format":"\\DYYYYMMDD\\THHmmss","locale":"fr_FR","output":"","outputType":"msg","outTz":"Europe/Paris","x":380,"y":260,"wires":[["44d8aedc.d4e41"]]},{"id":"44d8aedc.d4e41","type":"debug","z":"2bc4cb78.899144","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":260,"wires":[]}]

Without the leading \, D and T may refer to standard tokens, hence \D and \T.

2 Likes

Thanks about information....
So it is possible to choose...
Best regards
m.s.

Personally, even I wouldn't use the moment node to build a filename (and I wrote it!).

I would convert the Date object to an ISO string and add a replace function to remove the invalid characters. The output is still readable and still sorts correctly. No need for any complex formatting.

Also, abusing the Date class's prototype is definitely not recommended. It can cause massive hidden issues that are almost impossible to debug. I had a similar issue with another package a long time back that was doing something similar but the function wasn't quite correct and it was buried several layers down the package hierarchy. What a pain!

2 Likes

Thanks

Best regards
m.s.