User Tools

Site Tools


javascript:date_object_to_yyyymmdd

This is an old revision of the document!


JavaScript - Date object to YYYYMMDD

An easy way to turn a date object into a string in YYYYMMDD format, which is easier than concatenating Date.getYear(), Date.getMonth(), and Date.getDay().

Date.prototype.yyyymmdd = function() {
  var mm = this.getMonth() + 1; // getMonth() is zero-based
  var dd = this.getDate();
 
  return [this.getFullYear(), !mm[1] && '0', mm, !dd[1] && '0', dd].join(''); // padding
};
 
var date = new Date();
date.yyyymmdd();

This is a single line of code that you can use to create a YYYY-MM-DD string of today's date.

var d = new Date().toISOString().slice(0,10);
javascript/date_object_to_yyyymmdd.1467918925.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki