相关疑难解决方法(0)

Javascript日期解析错误 - 6月的日期失败(??)

我有一些解析ISO-8601日期的JavaScript.出于某种原因,6月份的日期失败了.但7月和5月的日期工作正常,这对我来说没有意义.我希望一双新的眼睛会有所帮助,因为我无法看到我在这里做错了什么.

功能定义(带bug)

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)

产量

  • 2009年5月9日星期六格林威治标准时间12:30:00
  • 星期四,2009年7月9 日12:30:00 GMT
  • 星期四,2009年7月9日12:30:00 GMT …

javascript parsing date

6
推荐指数
2
解决办法
3549
查看次数

从IE上的Date获取TimeZone字符串

谷歌浏览器

new Date()
//returns Fri Mar 29 2013 17:55:25 GMT+0530 (IST)
Run Code Online (Sandbox Code Playgroud)

IE8

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 给出了几分钟的偏移量,有没有办法使用偏移量来获取字符串?

或者我是否需要研究与偏移相对应的所有时区字符串并从中创建一个对象然后使用它?

javascript timezone internet-explorer date

6
推荐指数
1
解决办法
9237
查看次数

标签 统计

date ×2

javascript ×2

internet-explorer ×1

parsing ×1

timezone ×1