isISOString

JavaScript, Date

Checks if the given string is valid in the simplified extended ISO format (ISO 8601).

  • Use new Date() to create a date object from the given string.
  • Use Date.prototype.valueOf() and Number.isNaN() to check if the produced date object is valid.
  • Use Date.prototype.toISOString() to compare the ISO formatted string representation of the date with the original string.
const isISOString = val => {
  const d = new Date(val);
  return !Number.isNaN(d.valueOf()) && d.toISOString() === val;
};
Examples
isISOString('2020-10-12T10:10:10.000Z'); // true
isISOString('2020-10-12'); // false

Recommended snippets

  • toISOStringWithTimezone

    JavaScript, Date

    Converts a date to extended ISO format (ISO 8601), including timezone offset.

  • formatDuration

    JavaScript, Date

    Returns the human-readable format of the given number of milliseconds.

  • isDateValid

    JavaScript, Date

    Checks if a valid date object can be created from the given values.