Lua日期差异

我想知道你是否可以获取两个预定义或动态日期的日期差异。

使用此功能是否需要适当的日期格式?

函数 datediff(d1, d2, ...)

    col_date1 = os.time({year = d1:year(), month = d1:month(), day = d1:day() , hour = d1:hour(), min = d1:minute(), sec = d1:second() })
    col_date2 = os.time({year = d2:year(), month = d2:month(), day = d2:day() , hour = d2:hour(), min = d2:minute(), sec = d2:second() })

    local arg={...}
    if arg[1] ~= nil then
        if arg[1] == "min" then
            return math.abs((col_date1 - col_date2) / 60)
        elseif arg[1] == "hour" then
            return math.abs((col_date1 - col_date2) / 3600)
        elseif arg[1] == "day" then
            return math.abs((col_date1 - col_date2) / 86400)
        end
    end
    return math.abs(col_date1 - col_date2)
    --return 0
end

这是代码。但我不知道它是如何工作的。 输入应该类似于 31122017 - 31122016 是1年,或类似于此。

点赞
用户5287638
用户5287638

这段代码需要自定义日期对象作为输入。例如,如果您有一个代表日期的日期对象d,像2017年5月22日,那么调用d:year()将给您数字2017,d:hour()将给您数字5等等。

在标准的Lua中没有函数可以创建这样的对象,因此使用这段代码的项目必须使用一个单独的日期库。您需要找到如何创建您的项目所需的日期对象,然后将它们传递到函数中。

2017-05-22 02:58:04