Judging whether the date is legal in lua

Determine whether the date is legal

No more nonsense and go directly to the code

function isValidDate(year, month, day)
    if year < 1900 then
        print("年不合法")
        return false
    end
    if month < 1 or month > 12 then
        print("月不合法")
        return false
    end
    if day < 1 or day > 31 then
        print("日不合法")
        return false
    end
    local maxDay = 31
    if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12 then
        maxDay = 31
    elseif month == 2 then
            if year % 4 == 0 and year % 100 ~= 0 or year % 400 == 0 then
                maxDay = 29
            else
                maxDay = 28
            end
    else
        maxDay = 30
    end
    return day <= maxDay
end

Guess you like

Origin blog.csdn.net/weixin_56130753/article/details/126282698