-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate2.lua
More file actions
54 lines (41 loc) · 1.18 KB
/
date2.lua
File metadata and controls
54 lines (41 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
local now = os.time()
local targetZone = 8
--获取当前日期
function getDateByTimeZone(now,format)
local now = now or os.time()
local format = format or "%Y-%m-%d %H:%M:%S"
local utcdate = os.date("!*t", now)
local utcnow = os.time(utcdate)
local targetnow = utcnow+3600*targetZone
return os.date(format, targetnow)
end
--获取0点的时间戳
function getTodaySt(now)
local now = now or os.time()
local todayst = now - ((now+targetZone*3600)%86400)
return todayst
end
--获取当前星期几 使用通用格式时%w用0表示星期天。
function getWeekTime(now)
local now = now or os.time()
local week = getDateByTimeZone(now, "%w")
if tostring(week)=="0" then --星期天
week = 7
end
return tonumber(week)
end
--获取周一0点的时间戳
function getWeekStTime(now)
local now = now or os.time()
local week = getWeekTime(now)
return getTodaySt(now)-(week-1)*86400
end
local now = os.time()
print("星期:", getWeekTime(now))
local now = 1604160000
print("星期:", getWeekTime(now))
local now = 1604073600
print("星期:", getWeekTime(now))
local now = 1603987200
print("星期:", getWeekTime(now))
print("星期一时间戳:", getWeekStTime(os.time()))