# String replace - help please

**URL:** <https://discourse.nodered.org/t/string-replace-help-please/13388>\
**Category:** General\
**Created:** [17 July 2019 11:03 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388 "2019-07-17T11:03:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [17 July 2019 11:03 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388/1 "2019-07-17T11:03:17Z")

</div>

(The village idiot is out again)

I'm wanting to create a "payload" of the date/time.  
(Basically a log file name of meaning.)

This is the code I have:  
I'm going to do other things with it, but this is the basic model.  
The payload comes out with a space in it and that isn't exactly acceptable in file names.  
(Or I haven't worked out how to do that.... yet.)  
So I will replace the space with a `-`.  
It isn't working.

```auto
var now = new Date().toLocaleString();

var res = now.replace(' ', '-');

//msg.payload = new Date().toLocaleString();
msg.payload = now;

return msg;

```

This is what I get:  
`2019-7-17 21:01:41`

So, with what I know I'm trying.

Obviously I'm missing something.

Ok. Anyone - please.

---

<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:** [17 July 2019 11:13 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388/2 "2019-07-17T11:13:42Z")

</div>

You want an ISO format if you want to send a date/time as a string, not a locale format. If not sure how, check out the MDN site.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [17 July 2019 11:15 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388/3 "2019-07-17T11:15:57Z")

</div>

Thanks.

I also just realised that I will also need to get rid of the `:`'s as well.

Searching.

---

<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:** [17 July 2019 11:18 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388/4 "2019-07-17T11:18:26Z")

</div>

Ah, OK, you want it to be a filename. Yes, you will need to make it compatible.

Don't forget that ISO format will use the UTC timezone. This is generally best (you should only convert to local at point of user display, saves loads of issues).

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [17 July 2019 11:24 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388/5 "2019-07-17T11:24:29Z")

</div>

Yeah, GMT. I do kind of like it, but I am wanting/preferring local.

Then I can look at the file name and see it is (say) `2019-07-12-19-22-00` and I know it is 19:22 on 12 July.

Having to convert it from GMT is just another level of possible confusion for me.

I've found this link, but it _isn't playing fair_ with me.  
[how to get time as a string](https://stackoverflow.com/questions/3066586/get-string-in-yyyymmdd-format-from-js-date-object)

I'll keep at it and see if I can _stubborn my way_ through it.

This seems to be the _best fit_ to what I want, but when I try it, it doesn't seem to give what they claim.

I get a full date given back to me.

```auto
var today = new Date();
today.toISOString().substring(0, 10);

```

I get: (msg.payload = today)  
`Wed Jul 17 2019 21:26:22 GMT+1000 (AEST)`

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [17 July 2019 11:40 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388/6 "2019-07-17T11:40:30Z")

</div>

Ok, this is what I have now:

```auto
var d = new Date().toISOString().slice(0,19);
msg.payload = d;

return msg;

```

Works - kinda.

I get `2019-07-17T11:37:15` back. Which is nice, but is Zulu time.  
And it has `:`'s in it.

I'm getting there.

The _thing_ is that all these things have _magic_ commands which convert the date/time to "ISOString".... But ...... I can't _format_ how the digits are shown.  
The `.slice(0,19)` seems _redundant_ if I want all the time/date (or sorry: date time) shown.

Fun fun fun.

Oh, yeah:  
(by edit)  
So the code:

```auto
var d = new Date().toISOString().slice(0,19);

```

Got it. Assigns variable `d` with ...... `new Date()` new?  
How does that _work_.... (Semi rhetorical.)

---

<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:** [17 July 2019 11:45 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388/7 "2019-07-17T11:45:10Z")

</div>

Please explain what you want to see.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [17 July 2019 11:47 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388/8 "2019-07-17T11:47:59Z")

</div>

Ok, say it is:  
`2019/07/17 19:15:00:`  
I would like to get/see  
`2019-07-17-191500`

Or ...  
`T2019-07-17-191500`

I'm sort of open, but I hope you get what I want.

ALL TIMES ARE LOCAL

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [17 July 2019 11:51 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388/9 "2019-07-17T11:51:46Z")

</div>

> [@Trying\_to\_learn](#):
>
> Ok, say it is:  
> `2019/07/17 19:15:00:`  
> I would like to get/see  
> `2019-07-17-191500`
> 
> Or ...  
> `T2019-07-17-191500`

```auto
(new Date()).toISOString().substring(0,19).replace(/T/,"-").replace(/:/g,"")

```

will give `2019-07-17-115104`

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [17 July 2019 11:57 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388/10 "2019-07-17T11:57:51Z")

</div>

I was/am getting there.

This is what I had concocted in the mean time:

```auto
var today = new Date();
var d = new Date().toISOString().slice(0,19);
var res = d.replace(':', '-');
msg.payload = res;
return msg;

```

Where I make it a separate line. I am still coming to grips with all this concatanation of command thingies.

Putting my dumb hat on..... so what is this _magic_ `/T/` which is being replaced?  
I get that the date is something like 2019/7/17........ So the `replace(/T/,` means the `/`, as a `/` is a ...... _qualifier_ sort of thing. That replaces the `/` with `-` and then it replaces the `:`, which need to marked as `/:/g` with _blank_.....

I am not yet up to speed with all those _codes_ for those special characters.

Thanks very much Nick.  
And all others.

Oh.... So ok.  
The line:

> [@knolleary](#):
>
> (new Date()).toISOString().substring(0,19).replace(/T/,"-").replace(/:/g,"")

The name I use is.... `Date` or `new`? I'll work it out I guess.  
As I said, the way the commands are structured is way different to what/how I was shown way back.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [17 July 2019 12:02 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388/11 "2019-07-17T12:02:44Z")

</div>

Yes, I'm an idiot.

and I know I could have optimised this, but I am keeping it segmented just now for reasons I don't quite understand.

```auto
var d = (new Date()).toISOString().substring(0,19).replace(/T/,"-").replace(/:/g,"")

msg.payload = d;

return msg;

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [17 July 2019 12:07 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388/12 "2019-07-17T12:07:52Z")

</div>

```auto
var d = new Date().toISOString().slice(0,19);

```

This gets you `2019-07-17T11:59:27`

In order to get that to `2019-07-17-115927` you need to:

- replace the `T` in the middle with a `-`
- remove all of the `:`

The `replace` function ([documented here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace)) has two different ways of telling it _what_ you want to replace. You can either give it a string or a Regular Expression. In my response I used Regular Expressions which is why the have the`/` around them.

To replace the T we can do `d.replace("T","-")` or `d.replace(/T/,"-")` - they will have exactly the same result.

To replace the `:`, if you do `d.replace(":","")` then you would end up with `2019-07-17-1159:27`. This is because `replace` will only replace the _first_ occurrence of the string you give it.  
In this case we want it to replace _every_ occurrence of `:` - so for that we need to use the Regular Expression approach: `d.replace(/:/g,"")`. The magic here is the `g` at the end of the regex - that is a flag that will cause that regex to match _every_ instance and not just the first.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [17 July 2019 12:15 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388/13 "2019-07-17T12:15:11Z")

</div>

> [@knolleary](#):
>
> In this case we want it to replace _every_ occurrence of `:` - so for that we need to use the Regular Expression approach: `d.replace(/:/g,"")` . The magic here is the `g` at the end of the regex - that is a flag that will cause that regex to match _every_ instance and not just the first.

Thanks very much.

(Ok, my mistake. Left in for honesty)

B u t . .

If the `g` is the magic part to replace all the `:`'s.....  
Why/how does it work with the `/`s?  
. . . .

ARGH!

Dates aren't done 2019/07/17.  
They are ..... 2019-07-17.

Formatting.

So all I need to do is replace the `/T/` with a `-` and all the `:`'s.

I may have to stop soon, my "luck for today" is probably close to expiring.

Got it.

Thanks.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [17 July 2019 12:20 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388/14 "2019-07-17T12:20:18Z")

</div>

Um....

Yeah, I know I said it is solved.

I just realised: It isn't local time. ☹ I'm guessing that is just a . . . . . setting in how I get the time to make it local?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [17 July 2019 12:23 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388/15 "2019-07-17T12:23:16Z")

</div>

> [@Trying\_to\_learn](#):
>
> Dates aren't done 2019/07/17.  
> They are ..... 2019-07-17.

What exactly does `(new Date()).toISOString()` return for you?

For me, and according to [the documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString), the format of its output is `2019-07-17`. So where does your `2019/07/17` come from?

Regardless, applying what I've already explained, you can use `replace(/\//g,"-")` to convert them.

> [@Trying\_to\_learn](#):
>
> B u t . .
> 
> If the `g` is the magic part to replace all the `:` 's.....  
> Why/how does it work with the `/` s?

To be honest, I'm not going to type out a long explanation of what [Regular Expressions are or how they work](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions).

Suffice to say, the `/` characters mark the beginning and end of the regex, just like `"` marks the beginning and end of a string. Unlike strings however, Regular Expressions can have a set of flags set on them that change their behaviour. These flags are single-characters that follow the closing `/`.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [17 July 2019 12:29 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388/16 "2019-07-17T12:29:11Z")

</div>

> [@knolleary](#):
>
> What exactly does `(new Date()).toISOString()` return for you?

`2019-07-17T12:24:39.331Z` it is 22:24 local time.

> [@knolleary](#):
>
> For me, and according to [the documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString), the format of its output is `2019-07-17` . So where does your `2019/07/17` come from?

Me. Wholey and soley.

(excuse the spelling.)

That is how I write/format all my dates.

Dates are `/` times are `:` and there are no `-` used at all. Except here/now because it is a computer and those two letters are not allowed in file names.

> [@knolleary](#):
>
> Suffice to say, the `/` characters mark the beginning and end of the regex, just like `"` marks the beginning and end of a string. Unlike strings however, Regular Expressions can have a set of flags set on them that change their behaviour. These flags are single-characters that follow the closing `/` .

Nick, I'm not saying or implying you need to.  
I am only saying I am not familiar with this . . . . . style.

It is (basically) a foreign language.

I'll post now to get the time issue moving.

---

<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:** [17 July 2019 12:53 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388/17 "2019-07-17T12:53:42Z")

</div>

> [@Trying\_to\_learn](#):
>
> It is (basically) a foreign language.

It is indeed, it is "Regular Expression" language and it is a specialist grammar for handling text manipulation. Another useful tool in the toolbox.

> [@Trying\_to\_learn](#):
>
> That is how I write/format all my dates.

When dealing with ISO (International Standards Organisation) dates, the date parts are separated by "-" rather than "/". More importantly, the format is Year-Month-Day which is much more easily parsed by both humans and machines, is sortable sensibly and most important of all, is unequivocal unlike other formats where you can never be sure whether it is "m/d/y" or "d/m/y".

Personally, I always set my local date format in Windows to use "YYYY-MM-DD" so I never have to worry whether I'm looking at a US or UK date format. I've been bitten by that far too many times.

For bonus points, assuming you are using a reasonably up-to-date version of Node.js (8+ I suspect), I think that you can use the `Intl` internationalization library to format your Dates just how you want.

> **[Intl.DateTimeFormat - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat)**
>
> The Intl.DateTimeFormat object enables language-sensitive date and time formatting.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [17 July 2019 13:06 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388/18 "2019-07-17T13:06:38Z")

</div>

This works.

I know it isn't 100% what I said. But please understand as much as I want things to be perfect, I fully understand: that isn't going to happen.

```auto
var d = new Date().toLocaleString().substring(0,19).replace(/ /,".").replace(/:/g,"");
msg.payload = d;
return msg;

```

Gives me:  
`2019-7-17.230610`  
Which is well good enough for what I am needing.

Again: Thanks for all replies and help.

---

<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:** [17 July 2019 13:07 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388/19 "2019-07-17T13:07:49Z")

</div>

Note that Intl support in Node.js does need some care. Although the library is available, only the English ICU (the locale text) is included by default. You can specify additional ICU's at runtime:

[https://nodejs.org/docs/latest-v8.x/api/intl.html](https://nodejs.org/docs/latest-v8.x/api/intl.html)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [17 July 2019 13:13 UTC](https://discourse.nodered.org/t/string-replace-help-please/13388/20 "2019-07-17T13:13:08Z")

</div>

I'm not sure I really want to venture into that part of the world just yet.

I got that bit of code working and it is suffice.

I get that ZULU is a standard. But when looking at logs because something is going on, looking at logs with dates 10 hours ago, it gets confusing. So when making logs for debug or filenames that are date stamps, I like local time.

Hour:minute would be good, but I've been bitten there too. A couple of hours pass and you are wondering why things are as they are when you are looking at the wrong _hour_.  
HH:mm:ss is also handy, but for remote machines it can bite you with days passing.  
So YYYY/DD/MM HH:mm:ss is way preferred.  
I know that isn't _legal_ for file names etc.

But we've been over that.

I shall have to archive this new _gem_ of knowledge for how to cheat with date formatting.

The only nicer thing for it is to make any value \<10 formatted with a leading `0`

But that's for the future just now.

[Next page](https://discourse.nodered.org/t/string-replace-help-please/13388.md?page=2)
