這篇文章主要介紹的是利用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