Help me refactor my calendar program?

I knew this code was going to be bulky before I wrote it but I figured once I had something that worked it would be easier to make it smaller.

It just needs to progress through the year with respect to the amount of days in each month. It works, It’s just got a lot of duplicate code and despite my brain hardly functioning right now I’m confident there’s a neater way to do this. I can write the code my self I think if you give me some logical advice.

private void setDate()
    {
        //updating the day, month and year
        day++;
        if (day > 28 && month == 2 && year % 4 != 0) // Month 2, February, has 28 days on non-leap years, or years not divisible by 4.
        {
            month++;
            day = 1;
            if (month > 12)
            {
                year++;
                month = 1;
            }
        } else if (day > 29 && month == 2 &&year % 4 == 0) // Month 2, February, has 29 days on leap years, or years divisible by 4.
        {
            month++;
            day = 1;
            if (month > 12)
            {
                year++;
                month = 1;
            }
        } else if (day > 30 && month % 2 == 0 && month != 2) //Every month evenly divisible by 2, except February, has 30 days
        {
            month++;
            day = 1;
            if (month > 12)
            {
                year++;
                month = 1;
            }
        } else if (day > 31 && month % 2 == 1 && month != 2) //Odd months have 31 days, except February.
        {
            month++;
            day = 1;
            if (month > 12)
            {
                year++;
                month = 1;
            }
        }
    }

Edit: SetDate() is called in Update(). Also, I wanted to write this myself, without relying on the already available time functions.

Can’t you use datetime ?

1 Like

I want to write it myself. I don’t actually plan on using a Gregorian calendar for the whole project. That might sound ridiculous out of context but I have a good reason.

Anyways, I figured it out.

 private void setDate()
    {
        //updating the day, month and year
        day++;

        if (month == 2 && year % 4 != 0) { maxDays = 28; }          
        else if (month == 2 && year % 4 == 0) { maxDays = 29; }    
        else if (month % 2 == 0 && month != 2) { maxDays = 30; }
        else if (month % 2 == 1 && month != 2) { maxDays = 31; }

        if (day > maxDays)
        {
            month++;
            day = 1;
            if (month > 12)
            {
                year++;
                month = 1;
            }
        }
    }

Year % 4 won’t actually work for all cases of leap years, but if you’re not using a real calendar, that may not matter.

–Eric

Yeah my brother actually mentioned that to me. I looked into it further but I decided to leave it the way it is. My understanding is that I’m only losing a day every 100 years, but every 400 years I don’t lose a day. That’s too complicated.

I say we erect giant rockets to make the earth’s orbit exactly 365 days per year and forget about this non-sense.

Edit: Now that I think about it, it might be easier to correct the Earth’s rotation. lel