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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM