Module:Birthday:修订间差异

跳转到导航 跳转到搜索
创建页面,内容为“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…”
 
无编辑摘要
第31行: 第31行:
     local birthDay = tonumber(frame.args[3])
     local birthDay = tonumber(frame.args[3])


     if not birthYear or not birthDay then
    -- 检查缺失的输入
         return "输入的日期不完整"
     if not birthYear and not birthMonthInput and not birthDay then
         return "错误:输入的年份、月份和日期都是空的。"
     end
     end


第43行: 第44行:
     end
     end


     if not birthMonth then
    -- 生成日期字符串
         return "无效的月份"
    local dateString = ""
    end
 
     if birthYear and birthMonth and birthDay then
         if type(birthMonthInput) == "string" and (birthMonthInput:match("^[A-Za-z]+$")) then
            -- 英文月份格式
            dateString = string.format("%s %d, %d", birthMonthInput, birthDay, birthYear)
        else
            -- 中文或阿拉伯数字格式
            dateString = string.format("%d年%d月%d日", birthYear, birthMonth, birthDay)
        end
 
        -- 计算年龄
        local currentYear = tonumber(os.date("%Y"))
        local currentMonth = tonumber(os.date("%m"))
        local currentDay = tonumber(os.date("%d"))


    local currentYear = tonumber(os.date("%Y"))
        local age = currentYear - birthYear
    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


     if currentMonth < birthMonth or (currentMonth == birthMonth and currentDay < birthDay) then
        return string.format("%s (%d)", dateString, age)
         age = age - 1
     else
        -- 如果缺少年份、月份或日期中的任意一个,则只输出日期
        if birthYear and birthMonth then
            return string.format("%d年%d月", birthYear, birthMonth)  -- 年月
        elseif birthYear and birthDay then
            return string.format("%d年%d日", birthYear, birthDay) -- 年日
        elseif birthMonth and birthDay then
            return string.format("%d月%d日", birthMonth, birthDay)  -- 月日
        elseif birthYear then
            return string.format("%d年", birthYear)  -- 仅年份
         elseif birthMonth then
            return string.format("%d月", birthMonth)  -- 仅月份
        elseif birthDay then
            return string.format("%d日", birthDay)  -- 仅日期
        else
            return "错误:输入的年份、月份和日期都是空的。"
        end
     end
     end
    return age
end
end


return p
return p