Home > Article > Web Front-end > Javascript method to format string date into yyyy-mm-dd
This article mainly introduces the method of using Javascript to format a string date into yyyy-mm-dd. Not much to say below, refer to the following code
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('-'); }
Usage:
console.log(formatDate('Sun May 13,2016'));
Output:
2016-05-13 2014-05-11