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