Validate number of days in a given month

前端 未结 13 2114
独厮守ぢ
独厮守ぢ 2021-01-31 18:14

Performance is of the utmost importance on this one guys... This thing needs to be lightning fast!


How would you validate the number of days in a given mont

13条回答
  •  被撕碎了的回忆
    2021-01-31 18:20

    Assuming the JS Date object standard where months are numbered from 0, and you have your daysInMonth array:

    var days = daysInMonth[month] + ((month === 1) && (year % 4 === 0) && ((year % 100 !== 0) || (year % 400 === 0)));
    

    will give you the number of days in the month, with 28 increased to 29 iff the month is February and the year is a leap year.

提交回复
热议问题