lua中时间字符串转时间 其他时区时间转本地时区时间

复制代码
--字符串转时间戳 字符串格式:2019-12-24 15:53:23
function lxb.Common:getTimeStampByStirng(timeString)
    if type(timeString) ~= 'string' then print("string2time: timeString is not a string") return 0 end
    local fun = string.gmatch(timeString, "%d+")
    local y = fun() or 0
    if y == 0 then print("year == 0") return 0 end
    local m = fun() or 0
    if m == 0 then print("month == 0") return 0 end
    local d = fun() or 0
    if d == 0 then print("day == 0") return 0 end
    local H = fun() or 0
    if H == 0 then print("hour == 0") return 0 end
    local M = fun() or 0
    if M == 0 then print("minute == 0") return 0 end
    local S = fun() or 0
    if S == 0 then print("second == 0") return 0 end
    return os.time( { year = y, month = m, day = d, hour = H, min = M, sec = S })
end

--转换到本地时间 字符串格式
function lxb.Common:getLocalTimeWithString(timeString)
    local now = os.time()
    local dif =  os.difftime(now, os.time(os.date("!*t", now)))
    local time = os.date("*t", self:getTimeStampByStirng(timeString) + dif);
    local date = os.date("%Y-%m-%d %H:%M:%S", os.time(time));
    return date;
end

--转换到本地时间 table格式
function lxb.Common:getLocalTimeWithTab(timeString)
    local now = os.time()
    local dif =  os.difftime(now, os.time(os.date("!*t", now)))
    local date = os.date("*t", self:getTimeStampByStirng(timeString) + dif);
    return date;
end
复制代码

posted on   kk波波  阅读(1359)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示