English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Javascript Format String Date as yyyy-mm-dd Method

This article mainly introduces how to use JavaScript to format string dates in the format of yyyy-mm-The following is the method of dd, no more words, 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

Summary

That's all for this article. I hope the content of this article can be helpful to everyone's learning or use. If you have any questions, you can leave a message for communication. Thank you for your support of the呐喊 tutorial.

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously, and this website does not own the copyright, has not been manually edited, and does not assume relevant legal liabilities. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.

You May Also Like