Lua separated format string

Projects often encounter separate strings, such as "2019-11-23 12:23:56" That's how these numbers will separate out of it, directly below the code on to explain. Other similar situation

function split(input, delimiter)
   input = tostring(input)
    delimiter = tostring(delimiter)
    if (delimiter=='') then return false end
    local pos,arr = 0, {}
    -- for each pider found
    for st,sp in function() return string.find(input, delimiter, pos, true) end do
        table.insert(arr, string.sub(input, pos, st - 1))
        pos = sp + 1
        
    end
    table.insert(arr, string.sub(input, pos))
    return arr
end

function transfromDateToTime(datestr)
	if not datestr or datestr == "" then return 0 end
    local datelist = split(datestr, " ")
    local datebegin = split(datelist[1], "-")
    local dateend = split(datelist[2], ":")
    local day = tonumber(datebegin[3])
    local month = tonumber(datebegin[2])
    local year = tonumber(datebegin[1])
    local hour = tonumber(dateend[1])
    local minute = tonumber(dateend[2])
    local second = tonumber(dateend[3])

    print(year,month,day,hour,minute,second)
end

transfromDateToTime("2010-1-14 11:07:56")
Published 43 original articles · won praise 1 · views 2305

Guess you like

Origin blog.csdn.net/lpl312905509/article/details/103969863