最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery date conversion chrome works but IE and firefox don't - Stack Overflow

programmeradmin1浏览0评论

Lost here. The following works in Chrome wonderfully, but in IE and FireFox "undefined undefined NaN Nan" is returned

What am I missing?

var dateString = $(this).attr("ows_EventDate");
var current_date = new Date(dateString);
var month_names = [];
month_names[month_names.length] = "January";
month_names[month_names.length] = "February";
month_names[month_names.length] = "March";
month_names[month_names.length] = "April";
month_names[month_names.length] = "May";
month_names[month_names.length] = "June";
month_names[month_names.length] = "July";
month_names[month_names.length] = "August";
month_names[month_names.length] = "September";
month_names[month_names.length] = "October";
month_names[month_names.length] = "November";
month_names[month_names.length] = "December";
var day_names = [];
day_names[day_names.length] = "Sunday";
day_names[day_names.length] = "Monday";
day_names[day_names.length] = "Tuesday";
day_names[day_names.length] = "Wednesday";
day_names[day_names.length] = "Thursday";
day_names[day_names.length] = "Friday";
day_names[day_names.length] = "Saturday";

var startU = (day_names[current_date.getDay()]) + (", ") 
  + (month_names[current_date.getMonth()]) + (" ") + current_date.getDate() 
  + (" ") + (" ") + current_date.getFullYear();

Chrome returns... Thursday, February 23 2012 etc, etc just perfectly.

Lost here. The following works in Chrome wonderfully, but in IE and FireFox "undefined undefined NaN Nan" is returned

What am I missing?

var dateString = $(this).attr("ows_EventDate");
var current_date = new Date(dateString);
var month_names = [];
month_names[month_names.length] = "January";
month_names[month_names.length] = "February";
month_names[month_names.length] = "March";
month_names[month_names.length] = "April";
month_names[month_names.length] = "May";
month_names[month_names.length] = "June";
month_names[month_names.length] = "July";
month_names[month_names.length] = "August";
month_names[month_names.length] = "September";
month_names[month_names.length] = "October";
month_names[month_names.length] = "November";
month_names[month_names.length] = "December";
var day_names = [];
day_names[day_names.length] = "Sunday";
day_names[day_names.length] = "Monday";
day_names[day_names.length] = "Tuesday";
day_names[day_names.length] = "Wednesday";
day_names[day_names.length] = "Thursday";
day_names[day_names.length] = "Friday";
day_names[day_names.length] = "Saturday";

var startU = (day_names[current_date.getDay()]) + (", ") 
  + (month_names[current_date.getMonth()]) + (" ") + current_date.getDate() 
  + (" ") + (" ") + current_date.getFullYear();

Chrome returns... Thursday, February 23 2012 etc, etc just perfectly.

Share Improve this question edited Mar 7, 2012 at 4:39 kinakuta 9,0371 gold badge40 silver badges48 bronze badges asked Mar 7, 2012 at 4:36 CeeMoneyCeeMoney 2153 silver badges10 bronze badges 4
  • 2 what is the value for the value ing out of $(this).attr("ows_EventDate")? – Alastair Pitts Commented Mar 7, 2012 at 4:43
  • This jsFiddle jsfiddle/jfriend00/rgMmH works fine for me in Chrome, Firefox and IE9. Since you haven't included the value of $(this).attr("ows_EventDate");, I had to bypass that. – jfriend00 Commented Mar 7, 2012 at 4:47
  • Also, do you realize that this is a very inefficient way to declare your arrays. You can just do this; var day_names = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; – jfriend00 Commented Mar 7, 2012 at 4:49
  • I realize that it's horribly inefficient now. Newb = me :) The value that ing in, for the February example above, would be "2012-02-04 21:00:00" – CeeMoney Commented Mar 7, 2012 at 4:54
Add a ment  | 

2 Answers 2

Reset to default 6

dateString must be formatted correctly. ISO 8601 date formats should work (http://www.iso/iso/date_and_time_format)

In your ment you said dateString is equal to 2012-02-04 21:00:00. Replacing the space with a T would make it a valid date format that all browsers can parse, for example:

2012-02-04T21:00:00.

Example: http://jsfiddle/TQjhP/

Your date "2012-02-04 21:00:00" is not accepted by the Date() constructor in IE.

See this related post for details: Javascript Date() constructor doesn't work.

The spec for what the Date() constructor is supposed to accept as a string is RFC2822 if you really want the details of what is legal.

Apparently, Firefox and IE work with "2012/02/04 21:00:00".

See this article for further discussion.

发布评论

评论列表(0)

  1. 暂无评论