Appearance
时间方法
函数列表
1. 系统时间戳(毫秒) (systemTime)
说明: 返回系统当前时间戳,单位为毫秒
函数: systemTime()
参数: 无
返回值:
| 返回值类型 | 说明 |
|---|---|
| 整数 | 系统当前时间戳(毫秒) |
示例:
lua
local tm = systemTime()
print(tm)2. 获取网络时间字符串 (getNetWorkTime)
说明: 从网络获取当前时间并以 年-月-日_时-分-秒 的格式返回字符串
函数: getNetWorkTime()
参数: 无
返回值:
| 返回值类型 | 说明 |
|---|---|
| 字符串 | 网络时间字符串,格式为 年-月-日_时-分-秒 |
示例:
lua
local tm = getNetWorkTime()
print(tm) -- e.g. 2023-01-01_12-00-003. 脚本运行时间(毫秒) (tickCount)
说明: 返回脚本自启动以来的运行时长,单位为毫秒
函数: tickCount()
参数: 无
返回值:
| 返回值类型 | 说明 |
|---|---|
| 整数 | 脚本运行时间(毫秒) |
示例:
lua
local t = tickCount()
print(t)4. 日期格式化 (os.date)
说明: 用于格式化时间戳为可读时间字符串,time为可选时间戳,省略时取当前时间
函数: os.date(format [, time])
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| format | 字符串 | 格式化字符串 |
| time | 整数 | 时间戳(默认当前时间) |
返回值:
| 返回值类型 | 说明 |
|---|---|
| 字符串 | 格式化后的时间字符串 |
示例:
lua
local now = os.date("%Y-%m-%d %H:%M:%S")
--获取当前时间戳(秒)
local secs = os.time()
local year = string.sub(now,1,4)
local month = string.sub(now,6,7)
local day = string.sub(now,9,10)
local hour = string.sub(now,12,13)
local minute = string.sub(now,15,16)
local second = string.sub(now,18,19)
--获取7天前的时间
local ta = {
year=year,
month=month,
day=day-7,
hour=hour,
min=minute,
sec=second
}
--2018-05-02 09:50:57
local t = os.date("%Y-%m-%d %H:%M:%S", os.time(ta))
--获取今年2月份的天数
--3月份开始减去1天就是2月份的最后一天
local ta = {
year=year,
month=3,
day=0,
}
--28
local days = os.date("%d", os.time(ta))5. 获取时间戳 (os.time)
说明: 默认获取当前时间戳(秒),也可以指定table参数
函数: os.time([table])
参数:
| 参数名 | 类型 | 说明 |
|---|---|---|
| table | 表(table) | 时间表(包含year, month, day等字段) |
返回值:
| 返回值类型 | 说明 |
|---|---|
| 整数 | 时间戳(秒) |
示例:
lua
local time = os.time()
local hour = os.date("*t", time).hour
print(hour)
--获取指定时间的时间戳
local ta = {
year=2023,
month=1,
day=1,
hour=12,
min=0,
sec=0
}
local timestamp = os.time(ta)
print(timestamp)