Skip to content Skip to sidebar Skip to footer

How To Get Date In Format Yyyy-mm-dd In Javascript?

I have date in format: Thu, 01 Dec 2016 00:00:00 GMT, how to get date of format yyyy-mm-dd from that?

Solution 1:

If you want to stick to plain JS:

Date.prototype.yyyymmdd = function() {
  var mm = this.getMonth() + 1;
  var dd = this.getDate();

  return [this.getFullYear(),
          (mm>9 ? '' : '0') + mm,
          (dd>9 ? '' : '0') + dd
         ].join('-');
};

Solution 2:

if you want split it

var a = 'Thu, 01 Dec 2016 00:00:00 GMT'var s = a.split(' ')
console.log(s)
console.log(s[3]+'-'+s[2]+'-'+s[0])

Post a Comment for "How To Get Date In Format Yyyy-mm-dd In Javascript?"