Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Typescript将日期转换为如下字符串;20140127T224000Z”是什么;?_Javascript_Typescript - Fatal编程技术网

Javascript Typescript将日期转换为如下字符串;20140127T224000Z”是什么;?

Javascript Typescript将日期转换为如下字符串;20140127T224000Z”是什么;?,javascript,typescript,Javascript,Typescript,如何将日期对象转换为如下字符串:“20140127T224000Z” 我选择了日期对象time 当我执行selectedTime.toISOString() “2018-10-18T16:00:00.000Z” 然后,我应用替换函数: var date = selectedTime.toISOString().replace('-','').replace(':','').replace('.',''); 但由于某些原因,它不起作用 我尝试添加mo replace函数,但仍然得到相同的结果,

如何将日期对象转换为如下字符串:“20140127T224000Z”

我选择了日期对象
time

当我执行
selectedTime.toISOString()

“2018-10-18T16:00:00.000Z”

然后,我应用替换函数:

var date = selectedTime.toISOString().replace('-','').replace(':','').replace('.','');
但由于某些原因,它不起作用

我尝试添加mo replace函数,但仍然得到相同的结果,由于某种原因,“-”或“:”没有被删除

使用正则表达式也不起作用

以下是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');
    }
好的,我会保持简单,您如何解释:

使用

var date = selectedTime.toISOString().replace(/[-:.]/g,''); 
相反。如果需要,可以添加此定义,然后使用它:

String.prototype.replaceAll=函数(oldValue、newValue){
//看https://stackoverflow.com/a/6969486/2307070
var sanitizedOldValue=oldValue.replace(/[.*+?^${}()|[\]\\]/g,'\$&');
返回this.replace(newregexp(sanitizedOldValue,'g'),newValue);
}
var date=new date().toISOString().replaceAll('-','').replaceAll('.','').replaceAll(':','');
控制台日志(日期)如果提供了
字符串作为其第一个参数,则仅替换第一个出现的字符串

您需要传递带有global标志的正则表达式:

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

@ThomasAyoub Oops:3检查我的第二个屏幕截图,我应用了多次替换,仍然是相同的问题
date.toISOString().replace(/\W/gi')
我想知道这个用例的用法。有接受
20140127T224000Z
但不接受ISO标准的程序吗?google Calendar我不明白为什么simple string.replace不是working@monstro如回答中所述,如果您将字符串作为参数,则仅替换找到的第一个匹配项。因为字符串参数已转换为基本正则表达式,而不带“global”
g
说明符的正则表达式将只匹配一次。@monstro
replace('str','')的可能重复项将在内部生成正则表达式,但它将在第一次匹配时停止。使用
g
修饰符,它将替换所有发生的事件