Files
30-seconds-of-code/snippets/js/s/is-iso-formatted-date.md
Angelos Chalaris 9d032ce05e Rename js snippets
2023-05-19 20:23:47 +03:00

795 B

title, type, language, tags, author, cover, dateModified
title type language tags author cover dateModified
String is ISO formatted date snippet javascript
date
chalarangelo icebreaker 2020-11-29T12:16:43+02:00

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

  • Use the Date constructor 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;
};

isISOString('2020-10-12T10:10:10.000Z'); // true
isISOString('2020-10-12'); // false