模組:Date Convert
外觀
![]() | 此模組被引用於約28,000個頁面。 為了避免造成大規模的影響,所有對此模組的編輯應先於沙盒或測試樣例上測試。 測試後無誤的版本可以一次性地加入此模組中,但是修改前請務必於討論頁發起討論。 模板引用數量會自動更新。 |
本模組用於將各類日期轉換為ISO 8601格式或中文日期,以方便{{#time:params}}
函數和其他模板調用。
用法
輸出ISO日期
使用{{#invoke:Date_Convert|ISODate}}
函數。
輸入完整日期,只輸入年月、只輸入年份皆可:
{{#invoke:Date_Convert|ISODate|1994年10月26日}}
→ 1994-10-26{{#invoke:Date_Convert|ISODate|1994年10月}}
→ 1994-10{{#invoke:Date_Convert|ISODate|1994年}}
→ 1994
當月和日是個位數時,十位加「0」或不加「0」皆可,模組會自動補「0」,
{{#invoke:Date_Convert|ISODate|1994年8月1日}}
→ 1994-08-01{{#invoke:Date_Convert|ISODate|1994年08月01日}}
→ 1994-08-01
可以轉換英文和非規範的ISO 8601日期:
{{#invoke:Date_Convert|ISODate|October 26, 1994}}
→ 1994-10-26{{#invoke:Date_Convert|ISODate|26 Oct 1994}}
→ 1994-10-26{{#invoke:Date_Convert|ISODate|Sept 1994}}
→ 1994-09{{#invoke:Date_Convert|ISODate|1994-8-1}}
→ 1994-08-01{{#invoke:Date_Convert|ISODate|1994-8}}
→ 1994-08
如果字串以正確的日期開頭,則會轉換並忽視後綴文字:
{{#invoke:Date_Convert|ISODate|1994年10月26日,武漢}}
→ 1994-10-26{{#invoke:Date_Convert|ISODate|1994年10月26日,武漢|suffix=yes}}
→ 1994-10-26,武漢(將suffix
欄位設為yes
展示後綴)
以不合法日期開頭則報錯:
{{#invoke:Date_Convert|ISODate|一九九四年十月二十六日}}
→ 錯誤:時間格式不正確{{#invoke:Date_Convert|ISODate|1994年13月32日}}
→ 錯誤:時間格式不正確{{#invoke:Date_Convert|ISODate|一九九四年十月二十六日|error=ignore}}
→ 一九九四年十月二十六日(將error
欄位設為ignore
返回輸入值)
ISO 8601標準只規定了1583年之後的日期標準,請勿用本模組表示這之前的日期,否則可能會得到不期待的結果:
{{#invoke:Date_Convert|ISODate|25年8月5日(東漢建立)|suffix=yes}}
→ 0023-08-05(東漢建立)
而{{#invoke:ISODate|dates|xxx}}
等效於此{{#invoke:Date_Convert|ISODate|xxx}}
。
輸出中文日期
使用{{#invoke:Date_Convert|ChineseDate}}
函數。
輸入完整日期,只輸入年月、只輸入年份皆可:
{{#invoke:Date_Convert|ChineseDate|1994年10月26日}}
→ 1994年10月26日{{#invoke:Date_Convert|ChineseDate|1994年10月}}
→ 1994年10月{{#invoke:Date_Convert|ChineseDate|1994年}}
→ 1994年
當月和日是個位數時,十位加「0」或不加「0」皆可,模組會自動補「0」,
{{#invoke:Date_Convert|ChineseDate|1994年8月1日}}
→ 1994年8月1日{{#invoke:Date_Convert|ChineseDate|1994年08月01日}}
→ 1994年8月1日
可以轉換英文和非規範的ISO 8601日期:
{{#invoke:Date_Convert|ChineseDate|October 26, 1994}}
→ 1994年10月26日{{#invoke:Date_Convert|ChineseDate|26 Oct 1994}}
→ 1994年10月26日{{#invoke:Date_Convert|ChineseDate|Sept 1994}}
→ 1994年9月{{#invoke:Date_Convert|ChineseDate|1994-8-1}}
→ 1994年8月1日{{#invoke:Date_Convert|ChineseDate|1994-8}}
→ 1994年8月
如果字串以正確的日期開頭,則會轉換並忽視後綴文字:
{{#invoke:Date_Convert|ChineseDate|1994年10月26日,武漢}}
→ 1994-10-26
以不合法日期開頭則報錯:
{{#invoke:Date_Convert|ChineseDate|一九九四年十月二十六日}}
→ 錯誤:時間格式不正確{{#invoke:Date_Convert|ChineseDate|1994年13月32日}}
→ 錯誤:時間格式不正確{{#invoke:Date_Convert|ChineseDate|一九九四年十月二十六日|error=ignore}}
→ 一九九四年十月二十六日(將error
欄位設為ignore
返回輸入值)
ISO 8601標準只規定了1583年之後的日期標準,請勿用本模組表示這之前的日期,否則可能會得到不期待的結果:
{{#invoke:Date_Convert|ChineseDate|25年8月5日(東漢建立)}}
→ 23年8月5日
而{{#invoke:Date_Convert|ChineseDate|xxx}}
等效於此{{#invoke:Chinese_date|main|xxx}}
。
參見
require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local p = {}
local function error(template)
return '<strong class="error">错误:时间格式不正确</strong>[[Category:Template:' .. template .. '使用錯誤]]'
end
local function time(format, object)
return mw.getCurrentFrame():callParserFunction('#time', format, object)
end
local function convert(input)
input = input
:gsub(" "," ")
:gsub("%s+"," ")
:gsub('(%a+)[ ,]*(%d+)', '%1 %2');
local y, m, d, suf
local datePatternList = {
-- English date format
{'%d%d? ?%a+[ ,]*%d+', 'Y-m-d'}, -- 26 Oct 1994
{'%a+ ?%d%d?[ ,]+%d+', 'Y-m-d'}, -- Oct 26, 1994
{'%a+[ ,]*%d%d%d%d+', 'Y-m'}, -- Oct 1994
{'%a+ ?%d%d?', 'Y-m-d'}, -- Oct 26
{'%d%d? *%a+', 'Y-m-d'}, -- 26 Oct
-- Slash or hyphen date format
{'%d+/%d%d?/%d+', 'Y-m-d'}, -- 1994/10/26 or 10/26/1994
{'%d+%.%d%d?%.%d+', 'Y-m-d'}, -- 1994.10.26 or 26.10.1994
{'%d%d?/%d%d?', 'Y-m-d'}, -- 10/26
{'%d+%-%d%d?%-%d+', 'Y-m-d'}, -- 1994-10-26 or 26-10-94
{'%d%d%d%d+%-%d%d?', 'Y-m'}, -- 1994-10
{'%d%d%d%d', 'Y'}, -- 1994
}
y, m, d, suf = string.match(input, '^(%d+)年(%d%d?)月(%d%d?)日(.*)$');
if y then
if #y < 4 then
y = string.rep(0, 4 - #y) .. y
end
return time('Y-m-d', y .. '-' .. m .. '-' .. d) , suf
end
y, m, suf = string.match(input, '^(%d+)年(%d%d?)月(.*)$');
if y then
if #y < 4 then
y = string.rep(0, 4 - #y) .. y
end
return time('Y-m', y .. '-' .. m) , suf
end
y, suf = string.match(input, '^(%d+)年(.*)$');
if y then
if #y < 4 then
y = string.rep(0, 4 - #y) .. y
end
return time('Y', y) , suf
end
m, d, suf = string.match(input, '^(%d%d?)月(%d%d?)日(.*)$');
if m and tonumber(m) then
return time('m-d', m .. '-' .. d) , suf
end
for _, value in ipairs(datePatternList) do
local str, suf = string.match(input, '^(' .. value[1] .. ')(.*)$');
if str then
return time(value[2], str), suf
end
end
return time('Y-m-d', 'error')
end
function p._converttime(input, showsuf)
local date, suf = convert(input)
if (yesno(showsuf)) then
return date .. suf
else
return date
end
end
function p._ChineseDate(args, error)
local date, suffix = convert(args[1])
local errorMessage = time('Y-m-d', 'error')
suffix = args.suf and suffix or ''
if string.match(date, '^%d+%-%d%d%-%d%d$') then
return time('Y年Fj日', date):gsub("^0+","") .. suffix
end
if string.match(date, '^%d+%-%d%d$') then
return time('Y年F', date):gsub("^0+","") .. suffix
end
if string.match(date, '^%d+$') then
return time('Y年', date):gsub("^0+","") .. suffix
end
if string.match(date, '^%d%d%-%d%d$') then
return time('Fj日', date):gsub("^0+","") .. suffix
end
if date == errorMessage and args.error == 'ignore' then
return args[1]
end
--- return error('Chinese_date')
return '<strong class="error">错误:时间格式不正确</strong>[[Category:Template:Chinese_date使用錯誤]]'
end
function p.ChineseDate(frame)
local args = getArgs(frame)
return p._ChineseDate(args)
end
function p._ISODate(args, error)
local returnval, suf = convert(args[1])
local errorMessage = time('Y-m-d', 'error')
if returnval == errorMessage and args.error == 'ignore' then
return args[1]
end
if returnval == errorMessage then
--- return error('ISODate')
return '<strong class="error">错误:时间格式不正确</strong>[[Category:Template:ISODate使用錯誤]]'
end
return returnval .. (args.suffix and suf or '')
end
function p.ISODate(frame)
local args = getArgs(frame)
return p._ISODate(args)
end
return p