Lua 倒计时器(包括月份和年份)

下面是 Corona SDK 使用 LUA 写的倒计时器的示例。 我如何添加天数、月份和年份?

local function updateTime()
-- 减少秒数
secondsLeft = secondsLeft - 1

-- 时间跟踪使用秒。我们需要把它转换成分钟和秒
local minutes = math.floor( secondsLeft / 60 )
local seconds = secondsLeft % 60

-- 使用字符串格式化将其变为字符串
local timeDisplay = string.format( "%02d:%02d", minutes, seconds )
clockText.text = timeDisplay
点赞
用户1266551
用户1266551

Days (and hours) would be trivial, but what about months and years? Since you have no timestamp telling us of how many seconds left to what, it's hard knowing exactly how many months, depending on the length of the months (28, 29, 30 or 31 days). The same with years if we consider leap years as well. In any case, here's something that might be sufficient:

local SECONDS_IN_HOUR = 60 * 60
local SECONDS_IN_DAY = 24 * SECONDS_IN_HOUR
local SECONDS_IN_MONTH = 30 * SECONDS_IN_DAY -- assuming an average of 30 days per month
local SECONDS_IN_YEAR = 365 * SECONDS_IN_DAY

local years = math.floor((secondsLeft / SECONDS_IN_YEAR) % 365)
local months = math.floor((secondsLeft / SECONDS_IN_MONTH) % 12)
local days = math.floor((secondsLeft / SECONDS_IN_DAY) % 30)
local hours = math.floor((secondsLeft / SECONDS_IN_HOUR) % 24)
local minutes = math.floor((secondsLeft / 60) % 60)
local seconds = secondsLeft % 60

天(和小时)很简单,但是月份和年份怎么办?由于没有时间戳告诉我们距离什么时间还有多少秒,所以很难知道确切的月份,这取决于月份的长度(28、29、30或31天)。如果我们考虑闰年的话,年份也是如此。在任何情况下,以下内容可能足够:

local SECONDS_IN_HOUR = 60 * 60
local SECONDS_IN_DAY = 24 * SECONDS_IN_HOUR
local SECONDS_IN_MONTH = 30 * SECONDS_IN_DAY -- 假设每个月平均为30天
local SECONDS_IN_YEAR = 365 * SECONDS_IN_DAY

local years = math.floor((secondsLeft / SECONDS_IN_YEAR) % 365)
local months = math.floor((secondsLeft / SECONDS_IN_MONTH) % 12)
local days = math.floor((secondsLeft / SECONDS_IN_DAY) % 30)
local hours = math.floor((secondsLeft / SECONDS_IN_HOUR) % 24)
local minutes = math.floor((secondsLeft / 60) % 60)
local seconds = secondsLeft % 60
2016-04-19 04:36:36