繁体   English   中英

怎么把这个日期“ 2016年3月9日星期三09:48:09 PST”转换成“ YYYY-MM-DD HH:mm:ss”格式?

[英]How to convert this date 'Wed Mar 9 09:48:09 PST 2016' to 'YYYY-MM-DD HH:mm:ss' format?

我试图将日期时间值从此格式Wed Mar 9 09:48:09 PST 2016转换为以下格式YYYY-MM-DD HH:mm:ss

我试图利用片刻,但这给了我警告。

"Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info.
Arguments: [object Object]
fa/<@http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:9493
ia@http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:10363
Ca@http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:15185
Ba@http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:15024
Aa@http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:14677
Da@http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:15569
Ea@http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:15610
a@http://localhost:1820/Resources/Scripts/Plugins/moment.min.js:7:41
@http://localhost:1820/Home/Test:89:29
jQuery.event.dispatch@http://localhost:1820/Resources/Scripts/Jquery/jquery.min.js:5225:16
jQuery.event.add/elemData.handle@http://localhost:1820/Resources/Scripts/Jquery/jquery.min.js:4878:6
"

根据https://github.com/moment/moment/issues/1407,我不应该尝试使用moment()来执行此操作,因为它不可靠。

如何将Wed Mar 9 09:48:09 PST 2016可靠地转换为以下格式YYYY-MM-DD HH:mm:ss

您可以尝试使用Date.toJSON()String.prototype.replace() trim()

 var date = new Date("Wed Mar 9 09:48:09 PST 2016").toJSON() .replace(/(T)|(\\..+$)/g, function(match, p1, p2) { return match === p1 ? " " : "" }); console.log(date); 

由于您为问题标记了“ ,因此我将使用“时刻”进行回答。

首先,弃用是因为您在分析日期字符串时未提供格式说明,并且该字符串不是时刻可以直接识别的标准ISO 8601格式之一。 使用格式说明符,它将正常工作。

var m = moment("Wed Mar 9 09:48:09 PST 2016","ddd MMM D HH:mm:ss zz YYYY");
var s = m.format("YYYY-MM-DD HH:mm:ss"); // "2016-03-09 09:48:09"

其次,认识到在上面的代码中, zz只是一个占位符。 Moment实际上并不能解释时区缩写,因为歧义太多 (“ CST”有5种不同的含义)。 如果需要将其解释为-08:00 ,那么您必须自己进行一些字符串替换。

幸运的是,似乎(至少根据您的要求)似乎根本不需要任何时区转换,因此上述代码可以完成这项工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM