2012-12-21 43 views
0

任何人都可以幫助我嗎?javascript milliseconds notaion在firefox中不工作

var current_date=new Date('2012/12/21 22:59:59.997'); 
var result=current_date.getTime(); 

我沒有得到在Firefox的結果,但它確實顯示在鉻,在FF它顯示無效的日期。

+0

與幾乎任何日期格式,只有在手動解析並在'Date'構造函數中提供數字時才能使其跨瀏覽器工作。 – Bergi

+0

那麼,無論如何,你的日期字符串的時區是什麼? – closure

回答

3

你應該能夠做到以下幾點(使用date.setMilliseconds):

var dateString = '2012/12/21 22:59:59.997'; 
var dateStringSplit = dateString.split('.'); 
var myDate = new Date(dateStringSplit[0]); 
myDate.setMilliseconds(dateStringSplit[1]); 
console.log(myDate); 
2

Firefox和其他一些瀏覽器(即Safari或Opera)不喜歡毫秒。

// Split off the part after the dot 
var current_date = new Date('2012/12/21 22:59:59.997'.split('.')[0]); 

// Works everywhere! 
var result = current_date.getTime(); 

如果你真的想用毫秒來工作,你必須在多個部分分裂的日期和使用new Date()這些。從MDN文檔,這裏的選項:

new Date(year, month, day [, hour, minute, second, millisecond]) 

或者,作爲h2ooooooo說,你可以使用裂棗的第二部分,並使用setMilliseconds

總的來說,你有很多解決方案。選擇一個至少會讓你惱火的人。

+0

時區呢?在不同的時區它會給出不同的結果。 – closure

+0

那麼?這不是這個問題。 –

+0

優雅。 Upvoted。 –

0

格式使用的是不是Date.parse

您可能需要分割字符串,並分別設置各部分的標準格式。另外,請正確地管理時間段,因爲它是看不出來哪個時區的日期字符串是

+0

即使標準格式不能跨''Date.parse'使用跨瀏覽器:-) – Bergi