Skip to content
DateTimeDaysInMonth

DateTimeDaysInMonth

Description

Number of days in month month (1..12) of year, accounting for leap years.

Success

Returns 28..31. Returns 0 if month is outside 1..12.

Failure

Cannot fail (out-of-range month yields 0).

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    u32 DateTimeDaysInMonth(i32 year, u32 month) {
        static const u8 lengths[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (month < 1 || month > 12)
    
    static bool test_days_in_month(void) {
        return DateTimeDaysInMonth(2024, 2) == 29 && DateTimeDaysInMonth(2023, 2) == 28 &&
               DateTimeDaysInMonth(2024, 4) == 30 && DateTimeDaysInMonth(2024, 1) == 31 &&
               DateTimeDaysInMonth(2024, 12) == 31 && DateTimeDaysInMonth(2024, 0) == 0 &&
    static bool test_days_in_month(void) {
        return DateTimeDaysInMonth(2024, 2) == 29 && DateTimeDaysInMonth(2023, 2) == 28 &&
               DateTimeDaysInMonth(2024, 4) == 30 && DateTimeDaysInMonth(2024, 1) == 31 &&
               DateTimeDaysInMonth(2024, 12) == 31 && DateTimeDaysInMonth(2024, 0) == 0 &&
               DateTimeDaysInMonth(2024, 13) == 0;
        return DateTimeDaysInMonth(2024, 2) == 29 && DateTimeDaysInMonth(2023, 2) == 28 &&
               DateTimeDaysInMonth(2024, 4) == 30 && DateTimeDaysInMonth(2024, 1) == 31 &&
               DateTimeDaysInMonth(2024, 12) == 31 && DateTimeDaysInMonth(2024, 0) == 0 &&
               DateTimeDaysInMonth(2024, 13) == 0;
    }
               DateTimeDaysInMonth(2024, 4) == 30 && DateTimeDaysInMonth(2024, 1) == 31 &&
               DateTimeDaysInMonth(2024, 12) == 31 && DateTimeDaysInMonth(2024, 0) == 0 &&
               DateTimeDaysInMonth(2024, 13) == 0;
    }
Last updated on