-2

How can I convert a Date object to string like this: "20140127T224000Z"

I have Date object selectedTime

When I do selectedTime.toISOString() I get

"2018-10-18T16:00:00.000Z"

Then, I apply replace function:

var date = selectedTime.toISOString().replace('-','').replace(':','').replace('.','');

but it doesnt work for some reason, returns

enter image description here

I tried to add mo replace functions, but I still get the same result, for some reason, the "-" or the ":" are not removed

enter image description here

Using regex, doesnt work either

enter image description here

Here is the code snippet in typescript:

    onTimeSelected(selectedTime: Date, events) {
        var date = selectedTime.toISOString().replace('-','').replace('-','').replace(':','').replace(':','').replace('.','');
        var redirectTo = 'https://calendar.google.com/calendar/r/eventedit?dates=' + date + '/' + date;
        window.open(redirectTo, '_blank');
    }

OK, I will keep it simple, how do you explain this:

enter image description here

enter image description here

monstro
  • 6,254
  • 10
  • 65
  • 111
  • I wonder about the use case for this. Are there programs that accept `20140127T224000Z` but not the ISO standard? – Shilly Oct 16 '18 at 14:43
  • google calendar – monstro Oct 16 '18 at 14:45
  • I dont understand why simple string.replace is not working – monstro Oct 16 '18 at 14:45
  • @monstro as explained in an answer, replace only replace the first occurrence found if you give a string as parameter. Because the string parameter it's converted to a basic regular expression, and a regular expression without 'global' `g` specifier will match only once. – Pac0 Oct 16 '18 at 14:48
  • Possible duplicate of [How to replace all occurrences of a string in JavaScript?](https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript) – Thomas Ayoub Oct 16 '18 at 14:48
  • Check my second screenshot, I applied replace several times, same result, debugger doesnt lie – monstro Oct 16 '18 at 14:49
  • @monstro I cannot copy paste the code you use in your debugger since it's an image, but using two `replace(':', '')` works fine for me in my dev console. `(new Date()).toISOString().replace('-','').replace(':','').replace('.','');` outputs `"201810-16T1453:23401Z"`, whereas `(new Date()).toISOString().replace('-','').replace(':','').replace('.','').replace(':', '');` outputs `"201810-16T145351936Z"` . (Chrome 69) – Pac0 Oct 16 '18 at 14:53
  • @monstro debugger *can* lie ;) . For instance, Chrome dev console evaluates objects / arrays lazily. So you could maybe use several console.log, and at the time you look each object you displayed, it's not the old version you wanted to see, but a newer version modified (just giving an example of possible source of confusion) – Pac0 Oct 16 '18 at 14:56
  • who voted to close ???? I clearly say that it doesnt work, not here not on your url – monstro Oct 16 '18 at 14:59
  • debugger doesnt lie, i see that then I run the app – monstro Oct 16 '18 at 15:01
  • Can you make it an [MCVE]? Did you read the link I provided? – Thomas Ayoub Oct 16 '18 at 15:01
  • I dont understnad whats the problem with replace function, I dont want to use Re3gex, replace must work, but it doesnt – monstro Oct 16 '18 at 15:01
  • as already said to you in another comment, `replace` uses a regex internally anyway. If you copy paste the code I gave in my comment with two `replace(':', '')` in a JavaScript console, it works fine, you can test it. Other have provided you snippets that give the correct output as well, you can just click the execute button. If this is something is not working in your case, then provide the code that doesn't work in the question itself (not in a screenshot please). – Pac0 Oct 16 '18 at 15:07
  • I dont want to use regex – monstro Oct 16 '18 at 15:34
  • I cannot reproduce what you show us, see [demo](https://jsfiddle.net/jL8wbpdc/). Maybe watch is lying to you. – Thomas Ayoub Oct 17 '18 at 09:30

2 Answers2

3

If you supply String#replace with a String as its first parameter, only the first occurence will be replaced.

You need to pass a Regex with the global flag instead :

var date = selectedTime.toISOString().replace(/[-:.]/g,'');
Zenoo
  • 12,670
  • 4
  • 45
  • 69
1

Use

var date = selectedTime.toISOString().replace(/[-:.]/g,''); 

instead. If you want you can add this definition and then use it:

String.prototype.replaceAll = function (oldValue, newValue) {
    // see https://stackoverflow.com/a/6969486/2307070
    var sanitizedOldValue = oldValue.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    return this.replace(new RegExp(sanitizedOldValue, 'g'), newValue);
}

var date = new Date().toISOString().replaceAll('-','').replaceAll('.','').replaceAll(':','');

console.log(date);
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • @monstro `replace('str', '')` build a regex internally but it'll stop at first match. Using the `g` modifier, it'll replace all occurences – Thomas Ayoub Oct 16 '18 at 14:50