# Problem with substr()

**URL:** <https://discourse.nodered.org/t/problem-with-substr/42137>\
**Category:** General\
**Created:** [6 March 2021 08:01 UTC](https://discourse.nodered.org/t/problem-with-substr/42137 "2021-03-06T08:01:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thewaver](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thewaver/32/11917_2.png) [@Thewaver](https://discourse.nodered.org/u/Thewaver)\
**Post date:** [6 March 2021 08:01 UTC](https://discourse.nodered.org/t/problem-with-substr/42137/1 "2021-03-06T08:01:37Z")

</div>

HELP!!!!  
Substr wont split a string on Node red but works in [codepen.io](http://codepen.io) (javascript dev app)??

The following works on JavaScript [nodepen.io](http://nodepen.io) but NOT on node red:  
**var tima = "2021-02-09T07:36:40:000Z.";**  
**const ss = Number(String(datec).substr(11, 2);**  
**console.log(ss);**  
This outputs 07 which is what you would expect

This is what is on node red but the var tima is replaced with msg.payload[0].datetime which exactly the same as 2021-02-09T07:36:40:000Z as above:.  
**var tima = msg.payload[0].datetime;**  
**msg.payload = Number(String(tima).substr(11, 2));**  
**return msg;**  
msg.payload outputs 07 which is what NOT what I expected.

Can any kind person tell me where I am going wrong. Thanks....

---

<div class="post-metadata">

**Author:** ![Thewaver](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thewaver/32/11917_2.png) [@Thewaver](https://discourse.nodered.org/u/Thewaver)\
**Post date:** [6 March 2021 08:02 UTC](https://discourse.nodered.org/t/problem-with-substr/42137/2 "2021-03-06T08:02:42Z")

</div>

MISTAKE:  
msg.payload outputs 20 which is NOT what I expected??

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [6 March 2021 08:40 UTC](https://discourse.nodered.org/t/problem-with-substr/42137/3 "2021-03-06T08:40:26Z")

</div>

> **var tima = "2021-02-09T07:36:40:000Z.";**

This is not a valid date format (should be `2021-02-09T07:36:40.000Z`), if it were valid, you could use a change node with a jsonata+moment expression to extract the day.

Instead of using substr you could split it by `T` and then by `-` and get the last element;

```auto
let d = tima.split("T")[0].split("-")[2]
msg.payload = d
return msg

```

_edit_ this extracts the wrong value I guess (topic was not clear on what you needed to extract), you can adapt the code to your liking, ie;

```auto
let d = tima.split("T")[1].split(":")[0]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [6 March 2021 09:53 UTC](https://discourse.nodered.org/t/problem-with-substr/42137/4 "2021-03-06T09:53:14Z")

</div>

Or you could convert the string to Date object then you can use any of the Date functions to extract what you want.  
`let date = new Date(msg.payload[0].datetime)`

But are you sure that the value in the payload is a string and not already a Date? Add the line

```auto
node.warn(`type is ${typeof msg.payload[0].datetime}`)

```

and see what it says.

---

<div class="post-metadata">

**Author:** ![Thewaver](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thewaver/32/11917_2.png) [@Thewaver](https://discourse.nodered.org/u/Thewaver)\
**Post date:** [6 March 2021 14:30 UTC](https://discourse.nodered.org/t/problem-with-substr/42137/5 "2021-03-06T14:30:52Z")

</div>

Hi, thanks for your help. I used Colin's suggestion and converted the string into a date object and it worked as I wanted.

---

<div class="post-metadata">

**Author:** ![FireWizard52](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/firewizard52/32/34508_2.png) [@FireWizard52](https://discourse.nodered.org/u/FireWizard52)\
**Post date:** [6 March 2021 17:53 UTC](https://discourse.nodered.org/t/problem-with-substr/42137/6 "2021-03-06T17:53:39Z")

</div>

Hi,

You said, that the expected result of your function would be 07. However that is not the case, it will be only 7.

I tested your following function:

**var tima = "2021-02-09T07:36:40:000Z.";  
msg.payload = Number(String(tima).substr(11, 2));  
return msg;**

If you omit Number the result of the function will be "07", which is a (sub) string.  
The 12 th character in the string is 0 and then the string consist of 2 characters , so "07".  
The Number() function converts it to a number, without the leading zero.

So the result is just a Number 7

---

<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:** [20 March 2021 17:54 UTC](https://discourse.nodered.org/t/problem-with-substr/42137/7 "2021-03-20T17:54:33Z")

</div>

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