标准的 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
Run Code Online (Sandbox Code Playgroud)
(未经测试!)
或做一个搜索“LUA日期解析”找到一个3次党库,会为你做到这一点。