How to validate a date?

前端 未结 12 1790
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 04:13

I\'m trying to test to make sure a date is valid in the sense that if someone enters 2/30/2011 then it should be wrong.

How can I do this with any date?

12条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 04:47

    Hi Please find the answer below.this is done by validating the date newly created

    var year=2019;
    var month=2;
    var date=31;
    var d = new Date(year, month - 1, date);
    if (d.getFullYear() != year
            || d.getMonth() != (month - 1)
            || d.getDate() != date) {
        alert("invalid date");
        return false;
    }
    

提交回复
热议问题