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