DateTime.AddYears taking leap years into consideration

Hi Experts

How do I add a year to a specific date taking leap years into consideration?

myDate.AddYears(1) seems to be working fine, e.g. if the base date is 30th September 2019 the result will be 30th September 2020 and if the base date is 29th February 2020 will return 28th February 2021.

But if the base date is 28th February 2019 the result will be 28th February 2020 - what I need it to be is 29th February 2020.

How to get around this?

Hi @jacchr

Check this

((year mod 4 = 0) and (year mod 100 <>0)) or (year mod 400 = 0)

Th@nks

@shwin.S

Thanks @AshwinS2 that helped me a bit further - I can now make a check on leap year and add 1 day if the date is 28th February. Not sure that this is the best way though.

Hi @jacchr,
I think this article will explain it:

(See note on example #2)

BR
/Klaus

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.