code: function isValidDate(str) {
    // str must have passed the ISO8601 check
    // this check is meant to catch invalid dates
    // like 2009-02-31
    // first check for ordinal dates
    var ordinalMatch = str.match(/^(\d{4})-?(\d{3})([ T]{1}\.*|$)/);
  
    if (ordinalMatch) {
      var oYear = Number(ordinalMatch[1]);
      var oDay = Number(ordinalMatch[2]); // if is leap year
  
      if (oYear % 4 === 0 && oYear % 100 !== 0) return oDay <= 366;
      return oDay <= 365;
    }
  
    var match = str.match(/(\d{4})-?(\d{0,2})-?(\d*)/).map(Number);
    var year = match[1];
    var month = match[2];
    var day = match[3];
    var monthString = month ? "0".concat(month).slice(-2) : month;
    var dayString = day ? "0".concat(day).slice(-2) : day; // create a date object and compare
  
    var d = new Date("".concat(year, "-").concat(monthString || '01', "-").concat(dayString || '01'));
    if (isNaN(d.getUTCFullYear())) return false;
  
    if (month && day) {
      return d.getUTCFullYear() === year && d.getUTCMonth() + 1 === month && d.getUTCDate() === day;
    }
  
    return true;
  }
docs: /* eslint-enable max-len