Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q857 How do I validate a date in the format YYMMDD?

You are here: irt.org | FAQ | JavaScript | Date | Q857 [ previous next ]

Try:

<script language="JavaScript"><!--
function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function isValidYYMMDD(yymmdd) {
    yymmdd += '';
    if (yymmdd.length != 6) return false;
    if (yymmdd != ((yymmdd - 0) + '')) return false;

    year  = yymmdd.substring(0,2) - 0;
    month = yymmdd.substring(2,4) - 1;
    day   = yymmdd.substring(4,6) - 0;

    (year < 70) ? year += 2000: year += 1900;

    var test = new Date(year,month,day);

    if ( (y2k(test.getYear()) == year) &&
         (month == test.getMonth()) &&
         (day == test.getDate()) )
        return true;
    else
        return false;
}

document.write(isValidYYMMDD('990229'));
//--></script>

©2018 Martin Webb