我有一个像“2016-04-13”这样的字符串。我需要将其更改为日期格式 “2016年4月13日”。我怎么能用JavaScript做到这一点?
我尝试了什么:
new Date(dateString);
new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);
答案 0 :(得分:1)
使用String.replace
函数和预定义月份名称列表的解决方案:
var dt = new Date("2016-04-13".replace("-","/")),
monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
var dateStr = dt.getDate() +" "+ monthNames[dt.getMonth()] +", "+ dt.getFullYear();
console.log(dateStr); // 13 April, 2016