How to validate a date?

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

    I just do a remake of RobG solution

    var daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
    var isLeap = new Date(theYear,1,29).getDate() == 29;
    
    if (isLeap) {
      daysInMonth[1] = 29;
    }
    return theDay <= daysInMonth[--theMonth]
    

提交回复
热议问题