Try this – you need to actually create a date object rather than parsing the string
NOTE: This will test the string AS YOU POSTED IT.
YYYY-MM-DDTHH:MN:SS.MSSZ
It will fail on valid ISO8601 dates like
- Date: 2018-10-18
- Combined date and time in UTC: 2018-10-18T08:04:30+00:00 (without the Z and TZ in 00:00)
- 2018-10-18T08:04:30Z
- 20181018T080430Z
- Week: 2018-W42
- Date with week number: 2018-W42-4
- Date without year: –10-18 (last in ISO8601:2000, in use by RFC 6350[2])
- Ordinal date: 2018-291
It will no longer accept INVALID date strings
function isIsoDate(str) {
if (!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(str)) return false;
const d = new Date(str);
return d instanceof Date && !isNaN(d.getTime()) && d.toISOString()===str; // valid date
}
console.log(isIsoDate('2011-10-05T14:48:00.000Z'))
console.log(isIsoDate('2018-11-10T11:22:33+00:00'));
console.log(isIsoDate('2011-10-05T14:99:00.000Z')); // invalid time part