获取某月天数

1) 自己计算

获取指定年月的天数

local m_MonthDaysMap = {
    31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
}

function TimeUtil.GetMonthDays(year, month)
    if 2 == month then
        if (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0) then
            return 29
        else
            return 28
        end
    else
        local days = m_MonthDaysMap[month]
        return days
    end
end

 

2) 使用api

--获取今年某个月天数
function GetMonthDays(month)
    local year = tonumber(os.date("%Y"))
    local date = { year=year, month=month+1, day=0 }
    local timestamp = os.time(date)
    local monthDays = os.date("%d", timestamp)
    return tonumber(monthDays)
end

 

--获取这个月天数
function GetThisMonthDays()
    local dateTime = os.date("*t", os.time())
    local timestamp = os.time({ year=dateTime.year, month=dateTime.month+1, day=0 })
    local monthDays = os.date("%d", timestamp)
    return tonumber(monthDays)
end

 

posted @ 2022-07-16 23:19  yanghui01  阅读(99)  评论(0)    收藏  举报