2

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.

kinakuta
  • 9,029
  • 1
  • 39
  • 48
CeeMoney
  • 215
  • 3
  • 10
  • 2
    what is the value for the value coming out of `$(this).attr("ows_EventDate")`? – Alastair Pitts Mar 07 '12 at 04:43
  • This jsFiddle http://jsfiddle.net/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 Mar 07 '12 at 04: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 Mar 07 '12 at 04:49
  • I realize that it's horribly inefficient now. Newb = me :) The value that coming in, for the February example above, would be "2012-02-04 21:00:00" – CeeMoney Mar 07 '12 at 04:54

2 Answers2

6

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

In your comment 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.net/TQjhP/

Kai
  • 9,038
  • 5
  • 28
  • 28
1

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.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979