如何在Lua中验证字符串是否是有效日期?

时间:2012-09-22 10:10:37

标签: validation date lua

s =“2/29/2010”

如何验证上述字符串是Lua中的有效日期?

5 个答案:

答案 0 :(得分:4)

标准的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 派对库。

答案 1 :(得分:4)

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

答案 2 :(得分:1)

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

答案 3 :(得分:1)

虽然巴特基尔的回答在逻辑上是有组织的,呈现的和清晰的,但它允许无意义的日期为零值,例如&#39; 00/12/20&#39;和&#39; 02/00/2012&#39;通过验证。我通过更改&#39;&lt;&#;检查月份,日期和年份为&#39;&lt; =&#39;。

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

答案 4 :(得分:0)

Jeff Drumm和Bart Kier的回答没有考虑到字符串可能根本不是约会。我添加了代码以返回false,以防在下面的代码中找不到Jeff Drumms回答的年/月/日。

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