我有一些解析ISO-8601日期的JavaScript.出于某种原因,6月份的日期失败了.但7月和5月的日期工作正常,这对我来说没有意义.我希望一双新的眼睛会有所帮助,因为我无法看到我在这里做错了什么.
function parseISO8601(timestamp)
{
var regex = new RegExp("^([\\d]{4})-([\\d]{2})-([\\d]{2})T([\\d]{2}):([\\d]{2}):([\\d]{2})([\\+\\-])([\\d]{2}):([\\d]{2})$");
var matches = regex.exec(timestamp);
if(matches != null)
{
var offset = parseInt(matches[8], 10) * 60 + parseInt(matches[9], 10);
if(matches[7] == "-")
offset = -offset;
var date = new Date();
date.setUTCFullYear(parseInt(matches[1], 10));
date.setUTCMonth(parseInt(matches[2], 10) - 1); //UPDATE - this is wrong
date.setUTCDate(parseInt(matches[3], 10));
date.setUTCHours(parseInt(matches[4], 10));
date.setUTCMinutes(parseInt(matches[5], 10) - offset);
date.setUTCSeconds(parseInt(matches[6], 10));
date.setUTCMilliseconds(0);
return date;
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
alert(parseISO8601('2009-05-09T12:30:00-00:00').toUTCString());
alert(parseISO8601('2009-06-09T12:30:00-00:00').toUTCString());
alert(parseISO8601('2009-07-09T12:30:00-00:00').toUTCString());
Run Code Online (Sandbox Code Playgroud)
new Date()
//returns Fri Mar 29 2013 17:55:25 GMT+0530 (IST)
Run Code Online (Sandbox Code Playgroud)
new Date()
//returns Fri Mar 29 17:48:46 UTC+0530 2013
Run Code Online (Sandbox Code Playgroud)
我需要(IST)从IE上的Date中提取部分,在Chrome上我可以dateString.substring提取它,但在IE上我不能这样做.
该方法 getTimeZoneOffset 给出了几分钟的偏移量,有没有办法使用偏移量来获取字符串?
或者我是否需要研究与偏移相对应的所有时区字符串并从中创建一个对象然后使用它?