Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and work.

aviboots(AT)netvision.net.il

Instant Grammar Checker - Correct all grammar errors and enhance your writing

BlueHost Web Hosting Black Friday
BlueHost Web Hosting Black Friday

Bonsai Boy of New York Bonsai Trees and Accessories

DreamCloud The Comfortable Luxury Mattress Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


Disclosure We are a professional Programming & Software Q&A website, that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

16,295 questions

21,766 answers

573 users

How to check a date is valid or not in JavaScript

3 Answers

0 votes
function isValidDate(d) {
    if (Object.prototype.toString.call(d) === "[object Date]") { 
        if (isNaN(d.getTime())) {  
           return false
        } else { 
              return true
          } 
    } 
}

let d1 = new Date(); 
let d2 = new Date(2020, 5, 21); 
let d3 = new Date('2020-555-19'); 
let d4 = new Date(2020, 5, 2222222221, 10, 42, 33);
let d5 = new Date(999999930000);
let d6 = new Date("May 21, 2020 16:53:00");

console.log(isValidDate(d1));
console.log(isValidDate(d2));
console.log(isValidDate(d3));
console.log(isValidDate(d4));
console.log(isValidDate(d5));
console.log(isValidDate(d6));


/*
run:

true
true
false
false
true
true

*/

 




answered May 21, 2020 by avibootz
0 votes
function isValidDate(d) {
    return d instanceof Date && isFinite(d)
}

let d1 = new Date(); 
let d2 = new Date(2020, 5, 21); 
let d3 = new Date('2020-555-19'); 
let d4 = new Date(2020, 5, 2222222221, 10, 42, 33);
let d5 = new Date(999999930000);
let d6 = new Date("May 21, 2020 16:53:00");

console.log(isValidDate(d1));
console.log(isValidDate(d2));
console.log(isValidDate(d3));
console.log(isValidDate(d4));
console.log(isValidDate(d5));
console.log(isValidDate(d6));


/*
run:

true
true
false
false
true
true

*/

 




answered May 21, 2020 by avibootz
0 votes
function isValidDate(d) {
    return d instanceof Date && !isNaN(d);
}

let d1 = new Date(); 
let d2 = new Date(2020, 5, 21); 
let d3 = new Date('2020-555-19'); 
let d4 = new Date(2020, 5, 2222222221, 10, 42, 33);
let d5 = new Date(999999930000);
let d6 = new Date("May 21, 2020 16:53:00");

console.log(isValidDate(d1));
console.log(isValidDate(d2));
console.log(isValidDate(d3));
console.log(isValidDate(d4));
console.log(isValidDate(d5));
console.log(isValidDate(d6));


/*
run:

true
true
false
false
true
true

*/

 




answered May 21, 2020 by avibootz
...