s = "2010 年 2 月 29 日"
如何验证上面的字符串是 Lua 中的有效日期?
标准 Lua 库中没有这样的东西,但可以自己轻松创建:
function is_valid_date(str)
-- perhaps some sanity checks to see if `str` really is a date
local m, d, y = str:match("(%d+)/(%d+)/(%d+)")
m, d, y = tonumber(m), tonumber(d), tonumber(y)
if d < 0 or d > 31 or m < 0 or m > 12 or y < 0 then
-- Cases that don't make sense
return false
elseif m == 4 or m == 6 or m == 9 or m == 11 then
-- Apr, Jun, Sep, Nov can have at most 30 days
return d <= 30
elseif m == 2 then
-- Feb
if y%400 == 0 or (y%100 ~= 0 and y%4 == 0) then
-- if leap year, days can be at most 29
return d <= 29
else
-- else 28 days is the max
return d <= 28
end
else
-- all other months can have at most 31 days
return d <= 31
end
end
(未经测试!)
或者搜索“lua date parsing”以找到可以为您执行此操作的 3 rd方库。
function checkDate(us_mdY)
local m, d, Y = us_mdY:match("(%d+)/(%d+)/(%d+)")
local epoch = os.time{year=Y, month=m, day=d}
local zeromdy = string.format("%02d/%02d/%04d", m, d, Y)
return zeromdy == os.date('%m/%d/%Y', epoch)
end
function isdate(value)
-- Check for a UK date pattern dd/mm/yyyy , dd-mm-yyyy, dd.mm.yyyy
-- My applications needs a textual response
-- change the return values if you need true / false
if (string.match(value, "^%d+%p%d+%p%d%d%d%d$")) then
local d, m, y = string.match(value, "(%d+)%p(%d+)%p(%d+)")
d, m, y = tonumber(d), tonumber(m), tonumber(y)
local dm2 = d*m*m
if d>31 or m>12 or dm2==0 or dm2==116 or dm2==120 or dm2==124 or dm2==496 or dm2==1116 or dm2==2511 or dm2==3751 then
-- invalid unless leap year
if dm2==116 and (y%400 == 0 or (y%100 ~= 0 and y%4 == 0)) then
return "valid"
else
return "invalid"
end
else
return "valid"
end
else
return "invalid"
end
end
虽然 Bart Kier 的响应在逻辑上组织、呈现和清晰,但它允许诸如“00/12/2011”和“02/00/2012”等零值的无意义日期通过验证。我通过将月、日和年的“<”检查更改为“<=”来更正它。
function is_valid_date(str)
-- perhaps some sanity checks to see if `str` really is a date
local m, d, y = str:match("(%d+)/(%d+)/(%d+)")
m, d, y = tonumber(m), tonumber(d), tonumber(y)
if d <= 0 or d > 31 or m <= 0 or m > 12 or y <= 0 then
-- Cases that don't make sense
return false
elseif m == 4 or m == 6 or m == 9 or m == 11 then
-- Apr, Jun, Sep, Nov can have at most 30 days
return d <= 30
elseif m == 2 then
-- Feb
if y%400 == 0 or (y%100 ~= 0 and y%4 == 0) then
-- if leap year, days can be at most 29
return d <= 29
else
-- else 28 days is the max
return d <= 28
end
else
-- all other months can have at most 31 days
return d <= 31
end
end
Jeff Drumm 和 Bart Kier 的回答没有考虑到字符串可能根本不是日期。我添加了代码以在下面的代码中找不到 Jeff Drumms 回答的年份/月份/日期时返回 false。
function IsValidDate(str)
-- perhaps some sanity checks to see if `str` really is a date
local y, m, d = str:match("(%d+)/(%d+)/(%d+)")
if y == nil or m == nil or d == nil
then
return false
end
m, d, y = tonumber(m), tonumber(d), tonumber(y)
if d <= 0 or d > 31 or m <= 0 or m > 12 or y <= 0 then
-- Cases that don't make sense
return false
elseif m == 4 or m == 6 or m == 9 or m == 11 then
-- Apr, Jun, Sep, Nov can have at most 30 days
return d <= 30
elseif m == 2 then
-- Feb
if y%400 == 0 or (y%100 ~= 0 and y%4 == 0) then
-- if leap year, days can be at most 29
return d <= 29
else
-- else 28 days is the max
return d <= 28
end
else
-- all other months can have at most 31 days
return d <= 31
end
end