# Convert yyyymmdd format within a string to epoc

**URL:** <https://discourse.nodered.org/t/convert-yyyymmdd-format-within-a-string-to-epoc/89478>\
**Category:** General\
**Created:** [14 July 2024 11:14 UTC](https://discourse.nodered.org/t/convert-yyyymmdd-format-within-a-string-to-epoc/89478 "2024-07-14T11:14:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danman](https://avatars.discourse-cdn.com/v4/letter/d/e495f1/32.png) [@Danman](https://discourse.nodered.org/u/Danman)\
**Post date:** [14 July 2024 11:14 UTC](https://discourse.nodered.org/t/convert-yyyymmdd-format-within-a-string-to-epoc/89478/1 "2024-07-14T11:14:57Z")

</div>

Hi all  
wondering if anyone could provide a method (function or change node) of taking a string like this from a payload " **SG3.0RS,ON,2024-7-12 14:36:10,0.0,0.0,11.8,38.5,46,41.4,1291,241.6,1287,Run,415,38.5,Start,2024-7-12 14:36:10,0,0**" and replacing the yyyymmdd values in it with the epoch value.  
Thank you in advance.  
Dan

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [14 July 2024 11:27 UTC](https://discourse.nodered.org/t/convert-yyyymmdd-format-within-a-string-to-epoc/89478/2 "2024-07-14T11:27:00Z")

</div>

Assuming the structure is consistent, the easiest way, if you know a bit of JavaScript, would be a function node. Split the string by comma's, extract the date/time entry then feed that into a JS Date, something like this (untested):

```auto
const splitMsg = msg.payload.split(',')
const dt = splitMsg[2]

let mydate = new Date(dt)

// to get the UNIX Epoch rather than a JS timestamp
mydate = mydate / 1000 

msg.payload = mydate

return msg

```

BTW, the Monaco editor will complain about the division as it believes that a JavaScript Date object is not a number. One of the many oddities of JS is that Dates ARE BOTH. 😀 Ignore or cast `mydate` to a number if you prefer.

---

<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:** [14 July 2024 11:49 UTC](https://discourse.nodered.org/t/convert-yyyymmdd-format-within-a-string-to-epoc/89478/3 "2024-07-14T11:49:04Z")

</div>

If you want to replace bothe `YYYY-MM-DD HH:mm:ss` values, here is a change node option.

```auto
[{"id":"34555c4b1ece4315","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"SG3.0RS,ON,2024-7-12 14:36:10,0.0,0.0,11.8,38.5,46,41.4,1291,241.6,1287,Run,415,38.5,Start,2024-7-12 14:36:10,0,0","payloadType":"str","x":110,"y":180,"wires":[["db89616e8c66ea88"]]},{"id":"db89616e8c66ea88","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"(\t $part := $split($$.payload, \",\")[2];\t $replace($$.payload, $part, $moment($part).format(\"x\"))\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":180,"wires":[["c9747f50f33b19c5"]]},{"id":"c9747f50f33b19c5","type":"debug","z":"d1395164b4eec73e","name":"debug 2562","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":470,"y":180,"wires":[]}]

```

This only works if Both date values are the same.

---

<div class="post-metadata">

**Author:** ![Danman](https://avatars.discourse-cdn.com/v4/letter/d/e495f1/32.png) [@Danman](https://discourse.nodered.org/u/Danman)\
**Post date:** [14 July 2024 11:59 UTC](https://discourse.nodered.org/t/convert-yyyymmdd-format-within-a-string-to-epoc/89478/4 "2024-07-14T11:59:18Z")

</div>

you guys are amazing thank you both for the quick responses

---

<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:** [28 July 2024 12:00 UTC](https://discourse.nodered.org/t/convert-yyyymmdd-format-within-a-string-to-epoc/89478/5 "2024-07-28T12:00:00Z")

</div>

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