在 JavaScript 中手动将 yyyy-MM-ddTHH:mm:ss.fffZ 转换为 DateTime
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14238261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert yyyy-MM-ddTHH:mm:ss.fffZ to DateTime in JavaScript manually
提问by Martín
I receive from a Webservice a String with a date in this format:
我从 Web 服务收到一个带有以下格式日期的字符串:
yyyy-MM-ddTHH:mm:ss.fffZ
I need to convert that String with JavaScript to a normal DateTime but without using the new Date('yyyy-MM-ddTHH:mm:ss.fffZ')because I'm using an old version of JavaScript that not support that conversion. I can split that string and get the:
我需要使用 JavaScript 将该字符串转换为普通的 DateTime 但不使用新的,Date('yyyy-MM-ddTHH:mm:ss.fffZ')因为我使用的是不支持该转换的旧版本的 JavaScript。我可以拆分该字符串并获得:
- Year
- Month
- Days
- Time
- 年
- 月
- 天
- 时间
but how to manipulate the time zone "fffZ"Any suggestions?
但如何操纵时区有"fffZ"什么建议吗?
采纳答案by Martín
I've founded the solution. Please check http://webcloud.se/log/JavaScript-and-ISO-8601/
我已经找到了解决方案。请检查http://webcloud.se/log/JavaScript-and-ISO-8601/
Date.prototype.setISO8601 = function (string) {
var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
"(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
"(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
var d = string.match(new RegExp(regexp));
var offset = 0;
var date = new Date(d[1], 0, 1);
if (d[3]) { date.setMonth(d[3] - 1); }
if (d[5]) { date.setDate(d[5]); }
if (d[7]) { date.setHours(d[7]); }
if (d[8]) { date.setMinutes(d[8]); }
if (d[10]) { date.setSeconds(d[10]); }
if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
if (d[14]) {
offset = (Number(d[16]) * 60) + Number(d[17]);
offset *= ((d[15] == '-') ? 1 : -1);
}
offset -= date.getTimezoneOffset();
time = (Number(date) + (offset * 60 * 1000));
this.setTime(Number(time));
}
回答by Jan Jongboom
Here's a one liner from John Resig:
这是John Resig的一个单线:
var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
回答by Paul S.
If you know it will be of this form (ISO 8601, wiki), you can parse with RegExp or string methods. Here is a RegExp example that lets you use timezone Z, +hhor +hh:mm.
如果您知道它将采用这种形式(ISO 8601,wiki),您可以使用 RegExp 或字符串方法进行解析。这里是一个正则表达式的例子,让您使用时区Z,+hh或者+hh:mm。
var dateString = '2013-01-08T17:16:36.000Z';
var ISO_8601_re = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{3}))?(Z|[\+-]\d{2}(?::\d{2})?)$/,
m = dateString .match(ISO_8601_re);
var year = +m[1],
month = +m[2],
dayOfMonth = +m[3],
hour = +m[4],
minute = +m[5],
second = +m[6],
ms = +m[7], // +'' === 0
timezone = m[8];
if (timezone === 'Z') timezone = 0;
else timezone = timezone.split(':'), timezone = +(timezone[0][0]+'1') * (60*(+timezone[0].slice(1)) + (+timezone[1] || 0));
// timezone is now minutes
// your prefered way to construct
var myDate = new Date();
myDate.setUTCFullYear(year);
myDate.setUTCMonth(month - 1);
myDate.setUTCDate(dayOfMonth);
myDate.setUTCHours(hour);
myDate.setUTCMinutes(minute + timezone); // timezone offset set here, after hours
myDate.setUTCSeconds(second);
myDate.setUTCMilliseconds(ms);
console.log(myDate); // Tue Jan 08 2013 17:16:36 GMT+0000 (GMT Standard Time)
回答by Chasky
momentjshas the answer to this and many other date problems you might have. While it isn't clear where and how you will user the needed date, neither the wanted format, I think momentjs can give you some of the needed tasks I would add the module to my solution and use as (below is parse.com cloud code):
momentjs可以解决这个问题以及您可能遇到的许多其他日期问题。虽然不清楚您将在哪里以及如何使用所需的日期,也不是想要的格式,但我认为 momentjs 可以为您提供一些所需的任务,我会将模块添加到我的解决方案中并用作(下面是 parse.com cloud代码):
Parse.Cloud.define("momentFormat", function(request, response){
var message;
var date = momento('2013-01-08T17:16:36.000Z');
response.success("original format date: " + date.format("YYYY-MM-DDTHH:mm:ss.SSSZ") + " new format date: " + date.format("dddd, MMMM Do YYYY, h:mm:ss a"));
});
Output:
输出:
{"result":"original format date: 2013-01-08T17:16:36.000+00:00 new format date: Tuesday, January 8th 2013, 5:16:36 pm"}

