Search code examples
c#arrayswinformsc#-4.0leap-year

Calculating a leap year without the leap year function


I need to calculate if the current year at the runtime of the program is a leap year (divisible by 4, not divisible by 100 but divisible by 400) but without using the DateTime.LeapYear property. Can anyone suggest anything?

//DateTimePicker code

    private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
    {
        DateTime now;
        int[] months = {31,28,31,30,31,30,31,31,30,31,30,31};
        now = DateTime.Now.Date;
        if (now.Year / 4 == 0 && now.Year / 400 == 0)
        {
            months(1) = 29;
        }
    }

Solution

  • I think this covers the three criteria:

    var year = now.Year;
    
    if (year % 4 == 00 && !(year % 100 == 0 && year % 400 != 0))
    {
        ....
    }