这篇文章主要介绍的是利用Javascript将字符串日期格式化为yyyy-mm-dd的方法,下面话不多说了,参考如下代码
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