Validate number of days in a given month

前端 未结 13 2117
独厮守ぢ
独厮守ぢ 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条回答
  •  梦毁少年i
    2021-01-31 18:47

    I agree with Moayad and TED. Stick with the lookup table unless the month is February. If you need an algorithm for checking leap years, wikipedia has two:

    if year modulo 400 is 0 then leap
     else if year modulo 100 is 0 then no_leap
     else if year modulo 4 is 0 then leap
     else no_leap
    
    A more direct algorithm (terms may be grouped either way):
    
    function isLeapYear (year):
     if ((year modulo 4 is 0) and (year modulo 100 is not 0)) or (year modulo 400 is 0)
      then true
     else false
    

提交回复
热议问题