os.time()

类型 函数
os.*
返回值 数字
修订版本 版本 2024.3703
关键词 os, time, 日期
另请参阅 os.date()
os.clock()
system.getTimer()

概述

当不带参数调用时,返回从 1970 年至今的当前时间(以秒为单位);或者返回由给定表指定的日期和时间表示的时间。

语法

os.time( table )
(可选)

指定要转换为秒数的时间的表。如果存在 table,它必须包含字段 yearmonthday,并且可以包含其他字段 hourminsecisdst(有关这些字段的说明,请参阅 os.date 函数)。

示例

local t = os.date( '*t' )  -- get table of current date and time
print( os.time( t ) )      -- print date & time as number of seconds
-- OUTPUT: 1287516614
 
t.min = t.min + 1          -- add one to the minute field
print( os.time( t ) )      -- print number of seconds (increases by 60 seconds)
-- OUTPUT: 1287516674

-- Get the number of seconds since Jan 1, 1970 for a specific date and time
local t = {}
t.year = 2018
t.month = 12
t.day = 25
t.hour = 0
t.min = 0
t.sec = 0
print( os.time( t ) )
-- OUTPUT: 1545714000