Date string within longer text string

Node Red novice

I have a string in a msg.topic with a date and time within it. An example is below:

Motion Detected from GardenEnd at 2022/9/24 17:43:05

This could also start with "Person Detected...." instead of "Motion Detected...".
My challenge is to format this into a string to be used as a filename but for sorting purposes, the date and time will need leading zeros in the month and day, where required.

Ideally, above would become:

GardenEnd_20220924_174305. mpeg

I think, even with my very basic novice understanding of the edit node, I can remove spaces and text I don't need. However, getting the date into the correct format is flummoxing me. Adding leading zeroes, where required, is the bit I cannot fathom.

I would have thought a regex could identify the string in the edit node but cannot work out what goes in the Replace With.

Also, edit nodes don't seem to have an option to add underscores or full stops.

I guess the whole string could be manipulated in one go with some json but, again, i am a bit of a novice, so have been using the edit node with many

You could try this in a function node.

let payload = msg.payload.split(/at\s|from\s|\/|:/).slice(1);  // create array and remove first item
payload.splice(2,1,payload[2].padStart(2,"0")); //replace index 2 with padded index 2
msg.payload = payload.join("").split(" ").join("_") + ".mpeg"; // rejoin and add _ and .mpeg
return msg;

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