# Date string within longer text string

**URL:** <https://discourse.nodered.org/t/date-string-within-longer-text-string/68185>\
**Category:** General\
**Created:** [24 September 2022 21:45 UTC](https://discourse.nodered.org/t/date-string-within-longer-text-string/68185 "2022-09-24T21:45:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stain3565](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stain3565/32/68179_2.png) [@stain3565](https://discourse.nodered.org/u/stain3565)\
**Post date:** [24 September 2022 21:45 UTC](https://discourse.nodered.org/t/date-string-within-longer-text-string/68185/1 "2022-09-24T21:45:11Z")

</div>

Node Red novice

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

```auto
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:

```auto
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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [24 September 2022 23:29 UTC](https://discourse.nodered.org/t/date-string-within-longer-text-string/68185/2 "2022-09-24T23:29:50Z")

</div>

You could try this in a function node.

```auto
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;

```

---

<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:** [9 October 2022 00:56 UTC](https://discourse.nodered.org/t/date-string-within-longer-text-string/68185/3 "2022-10-09T00:56:29Z")

</div>

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