打开/关闭搜索
搜索
打开/关闭菜单
1706
4999
924
6.3万
RIA | Wiki
导航
首页
最近更改
随机页面
MediaWiki帮助
上传文件
RIA社群
RIA官网&红报社
RIA论坛
打开/关闭外观设置菜单
通知
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。
user-interface-preferences
个人工具
登录
目前wiki关闭了自行注册账号的功能,如需注册账号,请查阅
Help:注册账号
。
查看“︁模块:TimeDiff”︁的源代码
来自RIA | Wiki
查看
阅读
查看源代码
查看历史
associated-pages
模块
讨论
更多操作
←
模块:TimeDiff
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于这些用户组的用户执行:
用户
、已验证用户
您必须确认您的电子邮件地址才能编辑页面。请通过
参数设置
设置并确认您的电子邮件地址。
您可以查看和复制此页面的源代码。
local p = {} -- 获取时间戳的辅助函数 local function getTimestamp(timeStr) if not timeStr or mw.text.trim(timeStr) == '' then timeStr = 'now' end timeStr = mw.text.trim(timeStr) if string.lower(timeStr) == 'now' then return os.time() end local lang = mw.language.getContentLanguage() local success, timestamp = pcall(function() return tonumber(lang:formatDate('U', timeStr .. '+08:00')) end) if success and timestamp then return timestamp else return nil end end function p.main(frame) local args = frame:getParent().args if not args['开始时间'] and not args['结束时间'] and not args['格式'] then args = frame.args end local startTimeStr = args['开始时间'] local endTimeStr = args['结束时间'] local formatType = tonumber(args['格式']) local startTs = getTimestamp(startTimeStr) local endTs = getTimestamp(endTimeStr) if not startTs or not endTs then return '<span class="error">时间格式错误</span>' end local diff = endTs - startTs -- 未指定格式,返回原始秒数 if not formatType then return math.floor(diff) end local absDiff = math.abs(diff) -- 基础单位计算 local totalDays = math.floor(absDiff / 86400) local remainder = absDiff % 86400 local hours = math.floor(remainder / 3600) remainder = remainder % 3600 local minutes = math.floor(remainder / 60) local seconds = remainder % 60 -- 年份计算逻辑 (仅当格式 >= 5 时启用) -- 定义 1年 = 365天 local years = 0 local days = totalDays if formatType >= 5 then years = math.floor(totalDays / 365) days = totalDays % 365 end -- 构建输出字符串 local output = "" -- 格式5:输出年份 if formatType >= 5 then output = output .. years .. "年" end -- 所有格式都有天数 (如果是格式5,这里的 days 已经是去掉了年份后的剩余天数) output = output .. days .. "天" -- 格式2及以上:输出小时 if formatType >= 2 then output = output .. hours .. "小时" end -- 格式3及以上:输出分钟 if formatType >= 3 then output = output .. minutes .. "分钟" end -- 格式4及以上:输出秒数 if formatType >= 4 then output = output .. seconds .. "秒" end return output end return p
该页面嵌入的页面:
模块:TimeDiff/doc
(
查看源代码
)
返回
模块:TimeDiff
。
查看“︁模块:TimeDiff”︁的源代码
来自RIA | Wiki