2012-03-07 83 views
2

Lost here。以下在Chrome中的作品奇妙,但在IE和FireFox 「undefined undefined NaN Nan」被退回jquery date conversion chrome works but IE and firefox do not

我錯過了什麼?

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(); 

鉻返回... 2012年2月23日星期四等,完美。

+0

Nan不是數字。 – joshua 2012-03-07 04:37:46

+2

從$(this).attr(「ows_EventDate」)''出來的值的值是多少? – 2012-03-07 04:43:44

+0

這個jsFiddle http://jsfiddle.net/jfriend00/rgMmH/適用於Chrome,Firefox和IE9。既然你沒有包含'$(this).attr(「ows_EventDate」);'的值,我不得不繞過它。 – jfriend00 2012-03-07 04:47:30

回答

6

dateString必須格式正確。 ISO 8601日期格式應該可以工作(http://www.iso.org/iso/date_and_time_format)

在你的評論中,你說dateString等於2012-02-04 21:00:00。用T替換空間將使其成爲所有瀏覽器都可以解析的有效日期格式,例如:

2012-02-04T21:00:00

例如:http://jsfiddle.net/TQjhP/

+0

明白了,欣賞jsfiddle吧 – CeeMoney 2012-03-07 05:11:18

1

您的日期"2012-02-04 21:00:00"未被IE中的Date()構造函數接受。

看到這個相關文章的詳細信息:Javascript Date() constructor doesn't work

Date()構造函數應該接受爲字符串的規範是RFC2822如果您確實需要什麼是合法的細節。

顯然,Firefox和IE使用"2012/02/04 21:00:00"

請參閱this article進一步討論。

+0

謝謝,現在有道理。 – CeeMoney 2012-03-07 05:10:54

相關問題