13 Oct 2011

Date validation and comparision as dd-MMM-yyyy in javascript

validateDate.months = {jan: 0, feb: 1, mar: 2, apr: 3, may: 4, jun: 5, jul: 6, aug: 7, sep: 8, oct: 9, nov: 10, dec: 11};
    function validateDate(string) //used to validate
    {
        var m = /^(\d\d)-(\w{3})-(\d{4})$/.exec(string);
        if (!m) return false;
        var month = validateDate.months[m[2].toLowerCase()];
        if (typeof month != "number") return false;
        var date = +m[1];
        var year = +m[3];
        var d = new Date(year, month, date);
        if (d.getDate() != date || d.getMonth() != month) return false;
        return true;
    }

    function GetMyDate(string)//used to cmpare
    {
        var m = /^(\d\d)-(\w{3})-(\d{4})$/.exec(string);
        if (!m) return false;
        var month = validateDate.months[m[2].toLowerCase()];
        if (typeof month != "number") return false;
        var date = +m[1];
        var year = +m[3];
        var d = new Date(year, month, date);
        return d;
    }

No comments:

Post a Comment