在 jQuery 和 Internet Explorer 中将字符串转换为日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4052660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert string to date in jQuery and Internet Explorer?
提问by Anders
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:
我想在 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>
The alert is only there for testing, and in Chrome and Firefox it returns a date object, but in IE I get NaN.
警报仅用于测试,在 Chrome 和 Firefox 中它返回一个日期对象,但在 IE 中我得到 NaN。
What's wrong, and how can I do this conversion so that it works in IE as well?
出了什么问题,我该如何进行这种转换才能使其在 IE 中也能正常工作?
回答by Matt K
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).
下面的代码接受格式为 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;
}
Hope that helps!
希望有帮助!
回答by castis
If its a string that looks like a date, use this.
如果它是一个看起来像日期的字符串,请使用它。
var followupDate = new Date(Date.Parse(jQuery(this).text().trim()));
I guess a question I should have asked is, what is the output of
我想我应该问的一个问题是,输出是什么
jQuery(this).text().trim()
?
?
回答by Anders
I figured it out: IE apparently did not accept the Swedish date format, so I did a string replace to a format it did accept:
我想通了: IE 显然不接受瑞典日期格式,所以我将字符串替换为它确实接受的格式:
var followupDate = new Date(datestring.replace('-', '/'));
var followupDate = new Date(datestring.replace('-', '/'));
Unfortunately this format wasn't accepted by Firefox, so I had to keep the original code for Chrome and Firefox, and then use a separate script for IE with conditional comments.
不幸的是,Firefox 不接受这种格式,因此我必须保留 Chrome 和 Firefox 的原始代码,然后为 IE 使用带有条件注释的单独脚本。
回答by Neil
I haven't tested this, but how about:
我还没有测试过这个,但是如何:
var followupdate = new Date(jQuery(this).text().trim().toString());
The "toString()
" should force it to be interpreted as a string; the Date object should accept the string as valid input, and it might prevent IE from throwing up on it.
" toString()
" 应该强制它被解释为一个字符串;Date 对象应该接受字符串作为有效输入,它可能会阻止 IE 抛出它。
回答by Markus Knappen Johansson
I use moment like this:
我使用这样的时刻:
new Date(moment(item.ToDate));
Works with Swedish dates as well '2013-01-05':
也适用于瑞典日期“2013-01-05”:
new Date(moment('2013-01-05'));