Module:Birthday

Selfice留言 | 贡献2025年1月2日 (四) 00:30的版本 (创建页面,内容为“local p = {} -- 中文数字转换为阿拉伯数字 local function chineseToNumber(chinese) local numbers = { ["零"] = 0, ["一"] = 1, ["二"] = 2, ["三"] = 3, ["四"] = 4, ["五"] = 5, ["六"] = 6, ["七"] = 7, ["八"] = 8, ["九"] = 9, ["十"] = 10, ["十一"] = 11, ["十二"] = 12 } return numbers[chinese] or nil end -- 英文月份转换为阿拉伯数字 local function englishToNumber(month) local months…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

此模块的文档可以在Module:Birthday/doc创建

local p = {}

-- 中文数字转换为阿拉伯数字
local function chineseToNumber(chinese)
    local numbers = {
        ["零"] = 0, ["一"] = 1, ["二"] = 2, ["三"] = 3,
        ["四"] = 4, ["五"] = 5, ["六"] = 6, ["七"] = 7,
        ["八"] = 8, ["九"] = 9, ["十"] = 10, ["十一"] = 11,
        ["十二"] = 12
    }
    return numbers[chinese] or nil
end

-- 英文月份转换为阿拉伯数字
local function englishToNumber(month)
    local months = {
        January = 1, February = 2, March = 3,
        April = 4, May = 5, June = 6,
        July = 7, August = 8, September = 9,
        October = 10, November = 11, December = 12,
        Jan = 1, Feb = 2, Mar = 3, Apr = 4,
        Jun = 6, Jul = 7, Aug = 8, Sep = 9,
        Oct = 10, Nov = 11, Dec = 12
    }
    return months[month] or nil
end

function p.calculateAge(frame)
    local birthYear = tonumber(frame.args[1])
    local birthMonthInput = frame.args[2]
    local birthDay = tonumber(frame.args[3])

    if not birthYear or not birthDay then
        return "输入的日期不完整"
    end

    -- 转换月份
    local birthMonth
    if tonumber(birthMonthInput) then
        birthMonth = tonumber(birthMonthInput)  -- 如果是阿拉伯数字
    else
        birthMonth = chineseToNumber(birthMonthInput) or englishToNumber(birthMonthInput)
    end

    if not birthMonth then
        return "无效的月份"
    end

    local currentYear = tonumber(os.date("%Y"))
    local currentMonth = tonumber(os.date("%m"))
    local currentDay = tonumber(os.date("%d"))

    local age = currentYear - birthYear

    if currentMonth < birthMonth or (currentMonth == birthMonth and currentDay < birthDay) then
        age = age - 1
    end

    return age
end

return p