2016-01-21 58 views
1

我得到了一段從xml文件中獲取日期的代碼片段。我發現了一個代碼轉換日期dd/mm/yyyy。此代碼在Google Chrome中正常工作,但在Firefox,IE或Edge中無法正常工作... 在瀏覽器中代碼無法正常工作,函數將返回NaN/NaN/NaN。但例如:在谷歌瀏覽器中,重新啓動了12/2/2016。 dateFormXml的格式爲yyyy/MM/DD和輸出格式爲DD/MM/YYYY 這裏是我的代碼:函數getDate在FireFox和IE中不起作用

function dateConverter(dateFromXml){ 
    function format(x){ 
     //if the day/month is smaller then 10 add a 0 in front of it (9->09) 
     return (s < 10) ? '0' + x : x; 
    } 
    var d = new Date(dateFormXml), 
     convertedDate = [format(d.getDate()), format(d.getMonth() + 1), d.getFullYear()].join('/'); 

    return convertedDate; 
} 

誰能幫我做這個跨瀏覽器嗎? :)

+2

你能解釋你的代碼嗎?看起來像缺少一些東西 - 'dateFromXml'沒有被使用,'date'沒有被定義 –

+1

,同時也發佈了樣本'dateFromXml'格式被傳遞給函數 – Nirus

+0

[Date構造函數可能在IE中返回NaN,但在Firefox和Chrome](http://stackoverflow.com/questions/2182246/date-constructor-returns-nan-in-ie-but-works-in-firefox-and-chrome) –

回答

0
function dateConverter(dateFromXml){ 
     function format(x){ 
      return (x < 10) ? '0' + x : x; 
     } 
     var modifiedDate = date.split('-').join('/'), 
      d = new Date(modifiedDate), 
      convertedDate = [format(d.getDate()), format(d.getMonth()), d.getFullYear()].join('/'); 

     return convertedDate + time; 
    } 

嗨,大家好,我 媒體鏈接解決我自己的問題,感謝所有誰留下評論。 現在我將解釋我的代碼。 我在FireFox做的是:我在'd'和'dateFromXml'上打印出我的結果。我在firefox看到'd'是無效日期,'dateFromXml'返回日期yyyy-mm-dd hh:mm:ss。所以我開始研究,我發現了一個非常有用的網頁(http://dygraphs.com/date-formats.html)在這個網頁上,你會發現所有種類的日期格式。在這個網頁上,我看到我從XML文件得到的日期格式無效,因爲它是用' - '而不是'/'寫的,所以我在' - '上分隔日期,然後加上'/' 。那是所有... :)

相關問題