参考如下代码
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
使用方法:
console.log(formatDate('Sun May 13,2016'));
输出:
2016-05-13
2014-05-11