Javascript/IE 日期时间错误

标签 javascript internet-explorer firefox

在 IE 中使用此代码:

if(!Date.prototype.toISOString) Date.prototype.toISOString = function(){
    var padZero = function(str, len){while(str.length < len) str = '0' + str; return str;};

    var ret = padZero(''+this.getUTCFullYear(), 4)
        + '-' + padZero(''+this.getUTCMonth(), 2)
        + '-' + padZero(''+this.getUTCDate(), 2)
        + 'T' + padZero(''+this.getUTCHours(), 2)
        + ':' + padZero(''+this.getUTCMinutes(), 2)
        + ':' + padZero(''+this.getUTCSeconds(), 2)
        + 'Z';
    alert(ret);
    return ret;
}

我收到以下错误..

执行 urlrewrite 查询时出错:err:FORG0001: 类似日期时间的值“2012-00-05T09:09:46Z”的词法形式非法,月份字段的值 0 无效。 [第 42 行第 9 列]

我已经尝试了对月份参数的多次修复,但似乎无法正确解决。因此,我们将非常感谢任何帮助。

顺便说一句:上面的代码在 Firefox 中运行得很好..看看吧??

最佳答案

getUTCMonth() 从零开始,因此 0 是一月。您可以向其中添加 1 以形成日期字符串:

+ '-' + padZero(''+(this.getUTCMonth()+1), 2)

来自MDN docs :

getUTCMonth - Returns the month (0-11) in the specified date according to universal time.

关于Javascript/IE 日期时间错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14154341/

相关文章:

javascript - 让javascript取消jsp页面中的提交

css 子 (>) 选择器在 IE8 中不起作用?

php - 为什么 google chrome 和 IE 会出现这种奇怪的行为?

javascript - FireFox 和 Google Chrome 中不同的 JavaScript 对象文字行为?

css - 表格在 Firefox 中看起来很奇怪,但在 Safari/Chrome 中看起来不错

javascript - 无法设置复选框 HTML/CSS 的样式

javascript - 使用echart为条形图(yAxis)提供多种颜色

javascript - IE 6 和多个按钮元素都发送它们的名称和值

CSS 浮雕(插图)文本

javascript - JQuery $.post 在某些 PC 上无法运行