【问题标题】:Convert string to date in jQuery and Internet Explorer?在 jQuery 和 Internet Explorer 中将字符串转换为日期?
【发布时间】:2010-10-29 14:14:58
【问题描述】:

我想在 jQuery 中将日期字符串转换为日期对象,下面的代码适用于 Chrome 和 Firefox,但不适用于 Internet Explorer:

<script type="text/javascript" charset="utf-8">
//Validate if the followup date is now or passed:
    jQuery.noConflict();
    var now = new Date();
    jQuery(".FollowUpDate").each(function () {
        if (jQuery(this).text().trim() != "") {
            var followupDate = new Date(jQuery(this).text().trim()); //Here's the problem
            alert(followupDate);
            if (followupDate <= now) {
                jQuery(this).removeClass('current');
                jQuery(this).addClass('late');
            }
            else {
                jQuery(this).removeClass('late');
                jQuery(this).addClass('current');
            }
        }
    });
</script>

警报仅用于测试,在 Chrome 和 Firefox 中它返回一个日期对象,但在 IE 中我得到 NaN。

出了什么问题,我该如何进行这种转换以使其在 IE 中也能正常工作?

【问题讨论】:

    标签: jquery date


    【解决方案1】:

    这个问题帮助我找到了解决日期转换问题的方法。我找到了一种无需使用单独的脚本或测试浏览器类型即可转换日期的方法。

    以下代码接受格式为 2011-01-01(年、月、日)的日期。

    function convertDate(stringdate)
    {
        // Internet Explorer does not like dashes in dates when converting, 
        // so lets use a regular expression to get the year, month, and day 
        var DateRegex = /([^-]*)-([^-]*)-([^-]*)/;
        var DateRegexResult = stringdate.match(DateRegex);
        var DateResult;
        var StringDateResult = "";
    
        // try creating a new date in a format that both Firefox and Internet Explorer understand
        try
        {
            DateResult = new Date(DateRegexResult[2]+"/"+DateRegexResult[3]+"/"+DateRegexResult[1]);
        } 
        // if there is an error, catch it and try to set the date result using a simple conversion
        catch(err) 
        { 
            DateResult = new Date(stringdate); 
        }
    
        // format the date properly for viewing
        StringDateResult = (DateResult.getMonth()+1)+"/"+(DateResult.getDate()+1)+"/"+(DateResult.getFullYear());
    
        return StringDateResult;
    }
    

    希望有帮助!

    【讨论】:

    • 这已经有点老了,我已经继续前进了 :-) 但无论如何谢谢,我会尝试一下!
    • 这基本上有效。现在假设我有两个变量,其返回值是从这个变量返回的......它们可以相互扣除吗?
    • 如果被这个函数返回,它们可能无法相互扣除,因为该函数返回一个字符串。我建议在调用此函数之前对日期进行任何数学运算。
    【解决方案2】:

    如果它是一个看起来像日期的字符串,请使用它。

    var followupDate = new Date(Date.Parse(jQuery(this).text().trim()));
    

    我想我应该问的一个问题是,

    的输出是什么
    jQuery(this).text().trim()
    

    ?

    【讨论】:

    • 不幸的是,这给了我完全相同的结果 (NaN)。 jQuery(this).text().trim() 的输出是表格单元格的内容,它是瑞典日期格式(“2010-10-30”)的日期字符串。 Firefox 和 Chrome 对此没有任何问题,立即将其变成日期对象,但 IE 似乎无法识别它什么的...
    【解决方案3】:

    我想通了:IE 显然不接受瑞典日期格式,所以我将字符串替换为它接受的格式:

    var followupDate = new Date(datestring.replace('-', '/'));

    不幸的是,Firefox 不接受这种格式,所以我不得不保留 Chrome 和 Firefox 的原始代码,然后为带有条件 cmets 的 IE 使用单独的脚本。

    【讨论】:

    • 或使用 isNan(date) 条件来分隔 ie/其他浏览器
    【解决方案4】:

    我还没有测试过这个,但是怎么样:

    var followupdate = new Date(jQuery(this).text().trim().toString());
    

    toString()”应强制将其解释为字符串; Date 对象应该接受该字符串作为有效输入,它可能会阻止 IE 抛出它。

    【讨论】:

      【解决方案5】:

      我使用这样的时刻:

      new Date(moment(item.ToDate));
      

      也适用于瑞典日期“2013-01-05”:

      new Date(moment('2013-01-05'));
      

      【讨论】:

        猜你喜欢
        • 2011-02-27
        • 2017-10-14
        • 1970-01-01
        • 1970-01-01
        • 2015-04-27
        • 2011-11-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多