Home > OS >  Cannot save ISO String as a filename due to colons
Cannot save ISO String as a filename due to colons

Time:09-16

I have this code which I need to save the result as a filename:

let date = new Date();
date.toISOString(); // for example: "2022-12-19T15:28:46.493Z"

final string would like like this:

let finalStringToSave: "test_2022-12-19T15:28:46.493Z.txt"

When I try to save this string I get an error ...cannot save file.

I have the feeling that this is due to the colons ":" as they are not allowed in a filename.

I've tried to replace all the colons to underscore "_" or dash "-" and I tried this:

let str = finalStringToSave.replace(/:\s*/g, "-"); 
console.log(str);

But for some reason it's not doing the replace.

How can I fix this?

CodePudding user response:

You can use it like this:

`test_${date.toISOString()}.txt`.replace(/:/g,"_")

Let me know if it helped.

  • Related