Format JavaScript date into yyyy-MM-dd


This post goes over how to format a JavaScript date into a yyyy-MM-dd (year-month-day) string:

Don’t forget to adjust your date according to timezone.

ISO String

With Date.prototype.toISOString():

new Date().toISOString().split('T')[0];

Which is equivalent to:

new Date().toISOString().slice(0, 10);

Locale String

With Date.prototype.toLocaleDateString():

new Date().toLocaleDateString('en-CA');

Date Methods

With date instance methods:

function formatDate() {
  const date = new Date();
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  return [year, month, day].join('-');
}


Please support this site and join our Discord!