日期构造函数在IE中返回NaN,但在Firefox和Chrome中有效

踏板:

我正在尝试用JavaScript构建一个小日历。我的日期在Firefox和Chrome中运行良好,但是在IE中,日期函数返回了NaN。

这是函数:

function buildWeek(dateText){
    var headerDates='';
    var newDate = new Date(dateText);

    for(var d=0;d<7;d++){
        headerDates += '<th>' + newDate + '</th>';
        newDate.setDate(newDate.getDate()+1);
    }                       

    jQuery('div#headerDates').html('<table><tr>'+headerDates+'</tr></table>');
}

dateText是当前星期的星期一,实际上是在php中以“ m,d,Y”格式设置的,例如"02, 01, 2010"

加勒特:

Date构造函数接受任何值。如果参数的原语[[值]]是数字,则创建的日期具有该值。如果原始[[value]]为String,则该规范仅保证Date构造函数和parse方法能够解析Date.prototype.toString和Date.prototype.toUTCString()的结果。

设置日期的可靠方法是构造一个日期并使用setFullYearsetTime方法。

例如:http//jibbering.com/faq/#parseDate

ECMA-262 r3没有定义任何日期格式。将字符串值传递给Date构造函数或Date.parse具有与实现相关的结果。最好避免。


编辑: 来自comp.lang.javascript常见问题解答的条目是:扩展ISO 8601本地日期格式 YYYY-MM-DD 可以通过 Date 以下方式解析为

/**Parses string formatted as YYYY-MM-DD to a Date object.
 * If the supplied string does not match the format, an 
 * invalid Date (value NaN) is returned.
 * @param {string} dateStringInRange format YYYY-MM-DD, with year in
 * range of 0000-9999, inclusive.
 * @return {Date} Date object representing the string.
 */

  function parseISO8601(dateStringInRange) {
    var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,
        date = new Date(NaN), month,
        parts = isoExp.exec(dateStringInRange);

    if(parts) {
      month = +parts[2];
      date.setFullYear(parts[1], month - 1, parts[3]);
      if(month != date.getMonth() + 1) {
        date.setTime(NaN);
      }
    }
    return date;
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

日期差异在Firefox和IE中有效,但在Chrome中返回NaN

带有javascript的HTML表单在IE中获得“ NaN”,但在Chrome中有效

window.open在Firefox中有效,但在IE或Chrome中不起作用

Rmarkdown flexdashboard值框在Chrome或IE中无法正确呈现(但在FireFox中有效)

Angular HTTP请求在Chrome中返回null,但在Firefox中有效

单一测试在PhantomJS中失败,但在Chrome和Firefox中有效

添加的间距在Firefox中有效,但在Chrome和Safari中不起作用

在Chrome或Firefox中,jquery $ .ajax调用会导致401未经授权的响应,但在IE中有效

元素上的過渡在 Chrome 中有效,但在 Firefox 中無效

应用于音频元素的样式在 firefox 和 safari 中有效,但在 chrome 中无效

Moment.js在Chrome上具有有效日期,但在IE或Firefox上无效

'innerText'在IE中有效,但在Firefox中不可用

使用 webdriver (C#) 上传文件在 Firefox 中有效,但在 IE 中无效

内联在 CSS 中的 SVG 在 Chrome/Edge 中有效,但在 Firefox 中无效

javascript mailto在chrome中有效,但在IE中不起作用

Selenium无法通过moveToElement在IE中单击适当的目标,但在Chrome中有效

伪元素在 Firefox 中有效,但在 Chrome 中无效,为什么?

解构错误对象在Chrome中有效,但在Firefox中无效。该怎么办?

我的专栏文章将在Firefox / Safari中换行,但在Chrome中有效

解析XML标签属性在Firefox中有效,但在Chrome中不起作用

CSS剪切路径在Firefox中有效,但在chrome中不可用

动态生成的html链接在Firefox中有效,但在Chrome中不起作用

Selenium 测试用例在 Firefox 中有效,但在 Chrome 中无效 - Headless Setup

jQuery Click 功能在 Firefox 中有效,但在 Chrome/Safari 中无效

水平滚动在IE11上有效,但在Chrome和Firefox上无效

jQuery UI droppable在IE和Firefox上不起作用(但在Chrome上有效)

在Kendo Scheduler议程视图上隐藏表格列-在Firefox / Chrome中有效,但在IE11中不起作用

表格不在DIV内(在IE 9中有效,但在Chrome或IE11中无效)

函数在查询中有效,但在约束中无效