Javascript 检查日期文本在momentjs中是否有毫秒和秒

Javascript 检查日期文本在momentjs中是否有毫秒和秒,javascript,momentjs,Javascript,Momentjs,我正在尝试从文本格式解析日期,以查看其中是否包含毫秒和秒。 例如 基本上,我正在尝试替换已注释的代码。来自d3.time-format的utcParse()将检查日期文本中是否包含毫秒和秒。我为momentjs库做了一些尝试,但似乎不起作用。通过使用正则表达式,您可以检查字符串的某些部分是否存在 如果使用字符串调用matchDateTime(),则会得到一个包含所有匹配组的数组 让text_min='2016-02-02 14:30'; 让text_sec='2016-02-02 14:30:

我正在尝试从文本格式解析日期,以查看其中是否包含毫秒和秒。 例如


基本上,我正在尝试替换已注释的代码。来自d3.time-format的utcParse()将检查日期文本中是否包含毫秒和秒。我为momentjs库做了一些尝试,但似乎不起作用。

通过使用正则表达式,您可以检查字符串的某些部分是否存在

如果使用字符串调用
matchDateTime()
,则会得到一个包含所有匹配组的数组

让text_min='2016-02-02 14:30';
让text_sec='2016-02-02 14:30:34';
让text_ms='2016-02-02 14:30:34.234';
函数matchDateTime(text){return text.match(/(\d{4})-(\d\d)-(\d\d)(\d\d):(\d\d)(?:(\d\d)(?:\。(\d\d\d)));}
函数haseconds(text){return!!matchDateTime(text)[6];}
函数hasMissicles(text){return!!matchDateTime(text)[7];}
函数testTimeString(文本){
log(文本,“hasSeconds=”,hasSeconds(文本));
log(文本,“hasmillises=”,hasmillises(文本));
}
testTimeString(text_min);
testTimeString(文本秒);
testTimeString(文本);

log(matchDateTime(text_ms))通过使用正则表达式,您可以检查字符串的某些部分是否存在

如果使用字符串调用
matchDateTime()
,则会得到一个包含所有匹配组的数组

让text_min='2016-02-02 14:30';
让text_sec='2016-02-02 14:30:34';
让text_ms='2016-02-02 14:30:34.234';
函数matchDateTime(text){return text.match(/(\d{4})-(\d\d)-(\d\d)(\d\d):(\d\d)(?:(\d\d)(?:\。(\d\d\d)));}
函数haseconds(text){return!!matchDateTime(text)[6];}
函数hasMissicles(text){return!!matchDateTime(text)[7];}
函数testTimeString(文本){
log(文本,“hasSeconds=”,hasSeconds(文本));
log(文本,“hasmillises=”,hasmillises(文本));
}
testTimeString(text_min);
testTimeString(文本秒);
testTimeString(文本);
log(matchDateTime(text_ms))
let text = '2016-02-02 14:30:34.234';
const timezone = 'America/Los_Angeles';
// const hasMS = utcParse('%Y-%m-%d %H:%M:%S.')(text);
// const hasSeconds = utcParse('%Y-%m-%d %H:%M:')(text);

const hasMS = !!moment.tz(text, 'YYYY-MM-DD HH:MM:ss.', timezone);
console.log('hasMS', hasMS);
const hasSeconds = !!moment.tz(text, 'YYYY-MM-DD HH:MM:', timezone);
console.log('hasSeconds', hasSeconds);