2014-08-27 61 views

回答

1

如果你想使用純JavaScript堅持

function formatDate(dateString){ 
    var monthNames = new Array("January", "February", "March", 
     "April", "May", "June", "July", "August", "September", 
     "October", "November", "December"); 

    var dateArray = dateString.split("/"); 
    var month = monthNames[dateArray[1]-1]; 
    var day = dateArray[0]; 
    var year = dateArray[2]; 
    return month + " " + day + ", " + year; 
} 
0
var text = "14/07/2014"; 
var output = text.replace(/(\d\d)\/(\d\d)\/(\d\d\d\d)/, function($0, $1, $2, $3) { 
    var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; 
    return monthNames[parseInt($2, 10) - 1] + " " + $1 + ", " + $3; 
}); 

完全未經測試,是的 - 正則表達式是雜亂的(應該使用大括號然而,這讀起來更容易),但應該正確地工作...

+0

'應該使用大括號' - 對,你是。 – SeinopSys 2014-08-27 15:44:39

相關問題