How to calculate the number of days between two given dates? (Leap year obstacle)

后端 未结 6 1041
情深已故
情深已故 2020-12-11 20:44
  • Any year evenly divisible by 400 is a leap year (e.g., 2000 was a leap year).
  • Any other year evenly divisible by 100 is not a leap year (e.g., 1700, 1800 and
6条回答
  •  醉梦人生
    2020-12-11 21:22

    Leap year algorithm from wikipedia:

    Pseudocode 1:

    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
    

    Pseudocode 2:

    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
    

提交回复
热议问题