本文介绍了在jQuery和Internet Explorer中将字符串转换为日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < script type =text / javascriptcharset =utf-8> 
//验证后续日期现在或传递:
jQuery.noConflict();
var now = new Date();
jQuery(。FollowUpDate)。each(function(){
if(jQuery(this).text()。trim()!=){
var followupDate = new Date(jQuery(this).text()。trim()); //这里是问题
alert(followupDate);
if(followupDate< = now){
jQuery ).removeClass('current');
jQuery(this).addClass('late');
}
else {
jQuery(this).removeClass('late' );
jQuery(this).addClass('current');
}
}
});
< / script>

该警报只在测试中,Chrome和Firefox中会返回一个日期对象,但是IE我得到NaN。



有什么问题,我该怎么做这个转换,以便在IE中也可以使用?

解决方案

这个问题帮助我找出一个解决方案来解决我正在转换日期的问题。我找到一种方法来转换日期而不使用单独的脚本或测试浏览器类型。



下面的代码接受格式为2011-01-01(年,月,日)。

  function convertDate(stringdate)
{
// Internet Explorer不喜欢
//所以可以使用正则表达式获取年,月和日
var DateRegex = /([^ - ] *) - ([^ - ] *) - ([^ - ] *)/;
var DateRegexResult = stringdate.match(DateRegex);
var DateResult;
var StringDateResult =;

//尝试以Firefox和Internet Explorer了解
的格式创建新日期try
{
DateResult = new Date(DateRegexResult [2] + /+ DateRegexResult [3] +/+ DateRegexResult [1]);
}
//如果有错误,抓住它并尝试使用简单的转换设置日期结果
catch(err)
{
DateResult = new Date(stringdate);
}

//正确格式化日期查看
StringDateResult =(DateResult.getMonth()+ 1)+/+(DateResult.getDate()+ 1) +/+(DateResult.getFullYear());

return StringDateResult;
}

希望有帮助!


I want to convert a date string to a date object in jQuery, and the code below works fine for Chrome and Firefox, but not in 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>

The alert is only there for testing, and in Chrome and Firefox it returns a date object, but in IE I get NaN.

What's wrong, and how can I do this conversion so that it works in IE as well?

解决方案

This question helped me figure out a solution to a problem I was having converting dates. I found a way to convert the date without using separate scripts or testing for browser type.

The code below accepts a date in the format 2011-01-01 (year, month, day).

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;
}

Hope that helps!

这篇关于在jQuery和Internet Explorer中将字符串转换为日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 15:30