从dateTime对象中删除:,正确的正则表达式

Removing : from dateTime object, proper regex for this?

本文关键字:正则表达式 dateTime 对象 删除      更新时间:2023-09-26

我正在使用dateTime创建一个文件夹,并希望将其用作文件夹名称的一部分。这里面有":"字符,我很难找到正确的正则表达式来过滤掉":"。例如,dateTime输出是"Wed May 20 20:16:42 CDT 2015",我希望它是"Wed May 20 201642 CDT 2015"

我有以下

var fso, fo, dc;
fso = new ActiveXObject("Scripting.FileSystemObject");
fo = fso.GetFolder("C:''examplefolder");
dc = fo.DateCreated; //this gives me "Wed May 20 20:16:42 CDT 2015"
dc = dc.replace(/:/g, "");

没有从datetime中取出:,我得到了一个错误。我哪里做错了?

看起来fo.DateCreated是一个没有replace方法的日期对象。

//use one of the appropriate methods from toString(), toLocaleString(), toUTCString() etc to get a string representation
dc = new Date(fo.DateCreated).toString(); //a workaround since dc.toString() is not working
dc = dc.replace(/:/g, "");

注意:以前没有使用过ActiveX,所以不要为什么dc.toString()不工作。