以下是用LUA编写的Corona SDK倒数计时器的示例。 我如何为此添加日,月和年?
local function updateTime()
-- decrement the number of seconds
secondsLeft = secondsLeft - 1
-- time is tracked in seconds. We need to convert it to minutes and seconds
local minutes = math.floor( secondsLeft / 60 )
local seconds = secondsLeft % 60
-- make it a string using string format.
local timeDisplay = string.format( "%02d:%02d", minutes, seconds )
clockText.text = timeDisplay
端
答案 0 :(得分:0)
天(和小时)将是微不足道的,但几个月和几年呢?由于您没有时间戳告诉我们剩下多少秒钟,因此很难确切知道多少个月,具体取决于月份的长度(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 -- 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