String replace - help please

(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.

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.

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.


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


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).

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

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.

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

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

Ok, this is what I have now:

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:

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

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

Please explain what you want to see.

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

Or ...

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


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

will give 2019-07-17-115104


I was/am getting there.

This is what I had concocted in the mean time:

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:

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.

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.

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

msg.payload = d;

return msg;
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) 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.


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?
. . . .


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


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.



Yeah, I know I said it is solved.

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

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

For me, and according to the documentation, 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.

To be honest, I'm not going to type out a long explanation of what Regular Expressions are or how they work.

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 /.

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

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.

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.

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

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.

1 Like

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.

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

Gives me:
Which is well good enough for what I am needing.

Again: Thanks for all replies and help.

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:

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.