Languages
[Edit]
EN

TypeScript - detecting invalid date

0 points
Created by:
Selina-Miranda
737

In this article, we're going to have a look at how to check if data is invalid in TypeScript.

Date correctness can be checked basing on internal state of Date object.

Incorrect date object returns:

  • false for call isFinite(myDate),
  • true for call isNaN(myDate),
  • NaN from myDate.getTime() method,
  • "Invalid Date" string for toString() method call.

Quick solution:

const correctDate = new Date(2022, 10, 15);
const incorrectDate = new Date(NaN);

//                                        true  - means correct
//                                        false - means incorrect
//
console.log(isFinite(+correctDate));   // true
console.log(!isNaN(+correctDate));     // true
console.log(isFinite(+incorrectDate)); // false
console.log(!isNaN(+incorrectDate));   // false

 

Look below to see different practical examples:

1. Date object test example

This approach is based on fact: isFinite method returns false for incorrect dates.

const isCorrectDate = (date: Date): boolean => {
  return date instanceof Date && isFinite(+date);

  // return date instanceof Date && !isNaN(+date);
};


// Usage example:

const correctDate = new Date(2020, 10, 15);
const incorrectDate = new Date(NaN);

console.log(isCorrectDate(correctDate));   // true
console.log(isCorrectDate(incorrectDate)); // false

2. getTime() or other getter test example

This approach is based on fact: incorrect date returns NaN values from getters. So we can make a simple test for example with getTime() method.

const isCorrectDate = (date: Date): boolean => {
  return date instanceof Date && isFinite(date.getTime());

  // return date instanceof Date && !isNaN(date.getTime());
};


// Usage example:

const correctDate = new Date(2020, 10, 15);
const incorrectDate = new Date(NaN);

console.log(isCorrectDate(correctDate));   // true
console.log(isCorrectDate(incorrectDate)); // false

3. toString() method test example

This approach is based on fact: incorrect date object returns "Invalid Date" string for toString() method call. It is good to call to string method with protoype API that prevents methods overriding.

const isCorrectDate = (date: Date): boolean => {
  if (date instanceof Date) {
    const text = Date.prototype.toString.call(date);
    return text !== 'Invalid Date';
  }
  return false;
};


// Usage example:

const correctDate = new Date(2020, 10, 15);
const incorrectDate = new Date(NaN);

console.log(isCorrectDate(correctDate));   // true
console.log(isCorrectDate(incorrectDate)); // false

4. Detecting correct date string or date object example

The example presented in this section allows checking the correctness of the date provided as a string or object. String date should be formatted according to one of the standards:

  • IETF-compliant RFC 2822 timestamps,
  • ISO 8601.

Run the following code to see the effect:

const isCorrectDate = (date: Date | string): boolean => {
  return isFinite(+(date instanceof Date ? date : new Date(date)));

  // return isFinite(date instanceof Date ? date : Date.parse(date));
  // return !isNaN(date instanceof Date ? date : new Date(date));
  // return !isNaN(date instanceof Date ? date : Date.parse(date));
};


// Usage example:

const date1 = '2020-10-15';
const date2 = '2020-10-x5'; // <-------- incorrect date
const date3 = 'This is not date'; // <-- incorrect date
const date4 = new Date(2020, 10, 15);
const date5 = new Date(NaN); // <------- incorrect date

console.log(isCorrectDate(date1)); // true
console.log(isCorrectDate(date2)); // false
console.log(isCorrectDate(date3)); // false
console.log(isCorrectDate(date4)); // true
console.log(isCorrectDate(date5)); // false

Alternative titles

  1. TypeScript - how to check if date is invalid?
  2. TypeScript - how to check if date is correct?
  3. TypeScript - checking date correctness
  4. TypeScript - detecting incorrect date
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join