Home > Web Front-end > JS Tutorial > body text

Javascript method to format string date into yyyy-mm-dd

高洛峰
Release: 2016-12-08 15:33:07
Original
2938 people have browsed it

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 = &#39;0&#39; + month;
  if (day.length < 2) day = &#39;0&#39; + day;
 
  return [year, month, day].join(&#39;-&#39;);
}
Copy after login

Usage:

console.log(formatDate(&#39;Sun May 13,2016&#39;));
Copy after login

Output:

2016-05-13
 
2014-05-11
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template