Top community members
All Wiki Articles Create Wiki Article

Many years me and my friends we were volunteers on my university doing programming classes for young people.

I got main conclusion: Young people are awesome, especially students, because they always bring new and fresh ideas :)
* I loved to do it but my time is limited

  I decided to create something that will help millions of students

Now I am here

co-founder

JavaScript - detecting invalid date

0 contributions
5 points

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

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:

// ONLINE-RUNNER:browser;

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

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 bases on fact: isFinite method returns false for incorrect dates.

// ONLINE-RUNNER:browser;

function isCorrectDate(date) {
    return date instanceof Date && isFinite(date);

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

// Usage example:

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

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

2. getTime() or other getter test example

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

// ONLINE-RUNNER:browser;

function isCorrectDate(date) {
    return date instanceof Date && isFinite(date.getTime());

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

// Usage example:

var correctDate   = new Date(2020, 10, 15);
var 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 for methods overriding.

// ONLINE-RUNNER:browser;

function isCorrectDate(date) {
    if (date instanceof Date) {
        var text = Date.prototype.toString.call(date);
        return text !== 'Invalid Date';
    }
    return false;
}

// Usage example:

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

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

4. Detecting correct date string or date object example

Example presented in this section allows to check correctness of date provided as string or object. String date should be formatetd according to one of the standards:

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

Run following code to see effect:

// ONLINE-RUNNER:browser;

function isCorrectDate(date) {
    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:

var date1 = '2020-10-15';
var date2 = '2020-10-x5'; // <-------- incorrect date
var date3 = 'This is not date'; // <-- incorrect date
var date4 = new Date(2020, 10, 15);
var 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

 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey ūüĎč
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more