Quantcast
Channel: Detecting an "invalid date" Date instance in JavaScript - Stack Overflow
Viewing all articles
Browse latest Browse all 58

Answer by John for Detecting an "invalid date" Date instance in JavaScript

$
0
0

None of the above solutions worked for me what did work however is

function validDate (d) {
        var date = new Date(d);
        var day = ""+date.getDate();
        if( day.length == 1)day = "0"+day;
        var month = "" +( date.getMonth() + 1);
        if( month.length == 1)month = "0"+month;
        var year = "" + date.getFullYear();

        return ((month + "/" + day + "/" + year) == d);
    }

the code above will see when JS makes 02/31/2012 into 03/02/2012 that its not valid


Viewing all articles
Browse latest Browse all 58

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>