Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,906 questions

51,838 answers

573 users

How to check a date is valid or not in TypeScript

1 Answer

0 votes
function isValidDate(d : any) {
    if (Object.prototype.toString.call(d) === "[object Date]") { 
        if (isNaN(d.getTime())) {  
           return false;
        } else { 
              return true;
          } 
    } 
    else {
      return false;
    }
}
 
let d1 = new Date(); 
let d2 = new Date(2021, 5, 21); 
let d3 = new Date('2021-555-19'); 
let d4 = new Date(2021, 5, 2222222221, 10, 42, 33);
let d5 = new Date(999999933000);
let d6 = new Date("May 21, 2021 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 Mar 10, 2022 by avibootz
edited Mar 10, 2022 by avibootz

Related questions

2 answers 129 views
3 answers 225 views
1 answer 99 views
1 answer 105 views
1 answer 135 views
...