IE 7 中 Javascript 日期函数的问题,返回 NaN

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3243546/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:50:13  来源:igfitidea点击:

Problem with Javascript Date function in IE 7, returns NaN

javascriptjqueryjsondatetwitter

提问by superwhatever

I have a twitter feed and I create a new date obj so I can format the date to my liking.

我有一个 twitter 提要,我创建了一个新的日期 obj,这样我就可以根据自己的喜好格式化日期。

var created = new Date(this.created_at)works in firefox and chrome but not in IE7. I seem to be having trouble passing the date through the new Date()function. It just returns undefined and NaN.

var created = new Date(this.created_at)适用于 Firefox 和 chrome,但不适用于 IE7。我似乎无法通过new Date()函数传递日期。它只返回 undefined 和 NaN。

Here is the code. If you try to test it out don't forget to include jquery. Thank you.

这是代码。如果您尝试对其进行测试,请不要忘记包含 jquery。谢谢你。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Twitter Test</title>
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" >

$(function(){
$.getJSON("http://twitter.com/statuses/user_timeline/google.json?count=1&callback=?", function(data){
    $.each(data, function(){
        var created = new Date(this.created_at)
        $("<div></div>").append("<ul><li>Unformatted: " + this.created_at + "</li><li>Formatted: " + created + "</li></ul>").appendTo("body")
    });

})  

})

</script>
</head>

<body>
</body>
</html>

采纳答案by Dagg Nabbit

You'll want to make sure the date is parsed as UTC, because otherwise javascript will interpret it as a date in your local timezone.

您需要确保将日期解析为 UTC,否则 javascript 会将其解释为您当地时区的日期。

The date looks like this: Tue Jul 13 23:18:36 +0000 2010

日期看起来像这样: Tue Jul 13 23:18:36 +0000 2010

You can parse it like this:

你可以这样解析:

function parseDate(str) {
  var v=str.split(' ');
  return new Date(Date.parse(v[1]+" "+v[2]+", "+v[5]+" "+v[3]+" UTC"));
} 

Which will give the correct date/time in the local timezone, for example: Tue Jul 13 2010 19:18:36 GMT-0400 (EDT)

这将在本地时区给出正确的日期/时间,例如: Tue Jul 13 2010 19:18:36 GMT-0400 (EDT)

So that should leave your code looking something like this:

所以这应该让你的代码看起来像这样:

$(function(){
  $.getJSON("http://twitter.com/statuses/user_timeline/google.json?count=1&callback=?", function(data){
    $.each(data, function(){
      var created = parseDate(this.created_at);
      $("<div></div>").append("<ul><li>Unformatted: " + this.created_at + "</li><li>Formatted: " + created + "</li></ul>").appendTo("body");
    });
  });
  function parseDate(str) {
    var v=str.split(' ');
    return new Date(Date.parse(v[1]+" "+v[2]+", "+v[5]+" "+v[3]+" UTC"));
  } 
});

回答by Brian Ellis

I've found the jQuery Globalization Plugindate parsing to work best. Other methods had cross-browser issues and stuff like date.js had not been updated in quite a while.

我发现jQuery Globalization Plugin日期解析效果最好。其他方法存在跨浏览器问题,并且诸如 date.js 之类的东西已经有一段时间没有更新了。

You also don't need a datePicker on the page. You can just call something similar to the example given in the docs:

您也不需要页面上的 datePicker。您可以调用类似于文档中给出的示例的内容:

$.parseDate('yy-mm-dd', '2007-01-26');

回答by EricAdeo

Here is what I made to correct this!

这是我为纠正这个所做的!

$tweetList.append('<p><span class="twitterdate">' + parseTwitterDate(item.created_at) + location + '</span></p>');

var month=new Array();
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";

function parseTwitterDate($stamp) {
    var v=$stamp.split(' ');
    var date = new Date(Date.parse(v[1]+" "+v[2]+", "+v[5]+" "+v[3]+" UTC"));
    var hour = date.getHours();
    var ampm = hour<12 ? ' AM' : ' PM';
    return date.getHours() +':'+ date.getMinutes() +' '+ ampm +' '+ date.getDate() +' '+ month[date.getMonth()] +' '+ date.getFullYear();
}

And this give me "19:38 PM 23 April 2012" on Chrome, IE and Firefox.

这在 Chrome、IE 和 Firefox 上给了我“2012 年 4 月 23 日晚上 19​​:38”。