How to account for leap years?

前端 未结 4 366
陌清茗
陌清茗 2020-12-06 09:58

I have some doubts about the leap years, how can I be sure that by using a formula like this

add.years= function(x,y){    
if(!isTRUE(all.equal(y,round(y))))         


        
4条回答
  •  天涯浪人
    2020-12-06 10:25

    A year is a leap year if:

    • Is divisible by 4.
    • Not if it is divisible by 100.
    • But is if it is divisible by 400.

    That is why 2000 was a leap year (although it's divisible by 100, it's also divisible by 400).

    But generally, if you have a library that can take of date/time calculations then use it. It's very complicated to do these calculations and easy to do wrong, especially with ancient dates (calendar reforms) and timezones involved.

提交回复
热议问题