2012-05-10 41 views
4

我在使用Google日曆API中的日期格式的datajs時遇到問題。日期時間格式,我相信是RFC3339,這是從日曆API使用datejs解析RFC3339的Javascript日期時間

2012-01-05T08:45:00Z 

返回的樣本日期時間這是從datejs文檔here

Date.parse('1985-04-12T23:20:50Z')   // RFC 3339 Formats 

但這只是返回null。

我假設我有datejs作爲

Date.today().next().friday() 

返回正常工作星期五2012年5月11日00:00:00 GMT + 0100(BST)

+0

您使用什麼瀏覽器和瀏覽器版本? IE7-8? Date.parse('1985-04-12T23:20:50Z')'在IE7-8中返回'NaN'。在FF中返回正確的值。 –

+0

@AndrewD。我在Chrome –

+0

@mplungjan中使用JavaScript控制檯,謝謝但這並沒有幫助,因爲我想使用datejs和datejs應該能夠使用1985-04-12T23:20:50Z。如果你去datejs.com,你可以在他們的網站上輸入一個日期,它會正常工作。 –

回答

6

解決:使用Date.parseExact OR this versionBug report

DEMO using date.js

DEMO using date-en-US.js


使用的第一個版本,我得到

null 
http://datejs.googlecode.com/files/date.js 
Line 13 

,當我把斷言了testsuite的:

// null 
console.log('Date.parse("1985-04-12T23:20:50Z")', 
    Date.parse('1985-04-12T23:20:50Z')); 


// my previous answer works 
console.log('Date.parse("1985-04-12T23:20:50Z".replace(...))', 
    Date.parse('1985-04-12T23:20:50Z' 
      .replace(/\-/g,'\/') 
      .replace(/[T|Z]/g,' ') 
    ) 
); 

// the test suite without the Z works 
console.log('1985-04-12T23:20:50', 
    new Date(1985,3,12,23,20,50).equals(Date.parse('1985-04-12T23:20:50'))); 

// but this one fails when not in the test suite  
try { 
    console.log('1985-04-12T23:20:50Z', 
    new Date(1985,3,12,23,20,50).equals(Date.parse('1985-04-12T23:20:50Z'))); 
} 
catch(e) { 
    console.log('1985-04-12T23:20:50Z',e.message); 
} 

以下是此問題的older answer不使用日期時。 js

+0

看來,Date.parseExact(「1985-04-12T23:20:50Z」,「yyyy-MM-ddTHH:mm:ssZ」)工作正常。 –

+0

根據http://www.datejs.com/test/date_and_time/index.html使用Date.parseExact更快(「1985-04-12T23:20:50Z」,「yyyy-MM-ddTHH:mm:ssZ 「) –

+0

因此,要麼使用上述版本,要麼使用parseExact。我不認爲速度是一個問題。 – mplungjan