2012-11-14 58 views
3

我有一個日期字符串「2012-11-14T06:57:36 + 0000」,我要轉換爲以下格式「2012年11月14日12:27」。我嘗試了很多解決方案,包括Convert UTC Date to datetime string Javascript。但沒有什麼可以幫助我。以下代碼在android中適用於我。但對於IOS它顯示爲無效日期轉換UTC日期時間字符串鈦

var date = "2012-11-14T06:57:36+0000"; 
//Calling the function 
date = FormatDate(date); 

//Function to format the date 
function FormatDate(date) 
{ 
    var newDate = new Date(date); 
    newDate = newDate.toString("MMMM"); 
    return (newDate.substring(4,21)); 
} 

誰能幫助我?在此先感謝

回答

6

所有瀏覽器不支持相同的日期格式。我們可以選擇最好的方法是分割字符串的分隔符 - 和:和每個所得陣列項目傳遞給Date構造函數,請參閱下面的功能

function FormatDate(date) 
{ 
    var arr = date.split(/[- :T]/), // from your example var date = "2012-11-14T06:57:36+0000"; 
    date = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], 00); 
    newDate = date.toString("MMMM"); 
    //.. do further stuff here 
} 
+0

感謝Mejo,它爲我工作 – Anand

0

你可以得到一個Date對象通過初始化一個新的日期:

var date = "2012-11-14T06:57:36+0000"; 
var newDate = new Date(date); // this will parse the format 

console.log(newDate); 
> Wed Nov 14 2012 01:57:36 GMT-0500 (EST) 

至於格式,有幾個線程(如this one)上了。

還有一些用於日期格式化和處理的庫,人們通常在進行大量日期處理時最終使用這些庫。如果你正在尋找類似的東西,我會建議Datejs

+0

感謝您的回答,但它不適用於我在ios – Anand

相關問題