您的位置:首页 > 编程语言 > Lua

Lua的time相关函数

2016-03-08 11:44 344 查看

time函数

– 获取当前的格林尼治时间

os.time()

– 获取当前时间的秒表示 示例:1457412434

os.date()

–当前日期的字符串表示 示例:03/08/16 12:47:14

os.date(“%x”, os.time())

– %x 日期,示例:03/08/16

os.date(“%X”, os.time())

– %X 时间,示例:12:47:14

os.date(“%c”, os.time())

– %c 日期和时间 ,示例:03/08/16 12:47:14

os.clock()

–函数os.clock返回执行该程序CPU花去的时钟秒数

–示例

local x1 = os.clock()
local s = 0
for i = 1, 10000000 do
s = s + i
end
local x2 = os.clock()
print(string.format("cost time: %.2f\n", x2 - x1))


根据当前时间计算当日24点时间(get24Time()):

function get24Time(timeNow)
local time = timeNow --(当前时间)
local date = os.date("*t", time )
local next_date = {}
next_date.year = date.year
next_date.month = date.month
next_date.day = date.day + 1
next_date.hour = 0
next_date.min = 0
next_date.sec = 0
local time24Today = os.time(next_date)
return time24Today --当日24点的时间


计算两个时间戳之间的天数(getDayCounts()):

--使用上文的get24Time()
--getDayCounts()
function getDayCounts(time_before, time_now)
local old_time = time_before --第一个时间
local now_time = time_now --第二个时间戳
local old_time_to24 = get24Time(old_time)
local day_count = math.floor((now_time - old_time_to24)/86400)
return day_count --天数


版权声明: 本文为博主原创文章,未经博主允许不得转载。若本文内容侵权请告知,即刻删除。

如有错误,欢迎指出。 可在下方评论,亦可联系我。

Contact me:

QQ:812123870

e-mail:dxmdxm1992@gmail.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Lua time