How to validate a date?

前端 未结 12 1789
被撕碎了的回忆
被撕碎了的回忆 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:57

    function isValidDate(year, month, day) {
            var d = new Date(year, month - 1, day, 0, 0, 0, 0);
            return (!isNaN(d) && (d.getDate() == day && d.getMonth() + 1 == month && d.getYear() == year));
        }
    

提交回复
热议问题