Skip to content

Days in a Calendar Month

Return how many days a month has, using 29 for February in a Gregorian leap year and 0 when the month is out of range.

Language: C

Added on: 21st August 2026

int days_in_a_month(int month, int year)
{
    static const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    static const int leap_days[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    if (month < 1 || month > 12) {
        return 0;
    }
    return is_leap_year(year) ? leap_days[month - 1] : days[month - 1];
}

Caveats

Month is 1-12. Needs is_leap_year from the Gregorian leap year snippet.