我从jQuery BeatPicker获取日期值,如“14/07/2014”,我需要将此值格式化为“2014年7月14日”。
有没有简单的方法可以使用javascript / jQuery / plugin实现这个目标?
由于
答案 0 :(得分: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;
}
答案 1 :(得分:1)
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;
});
完全没有经过测试,是的 - 正则表达式很乱(应该使用大括号,但这样更容易阅读),但是应该正常工作......