Convert a string date to a timestamp

后端 未结 5 1643
无人及你
无人及你 2021-01-31 22:34

Is there any simple way to convert a RFC HTTP date into a timestamp in Lua?

\"Sat, 29 Oct 1994 19:43:31 GMT\"

into

783467011

5条回答
  •  猫巷女王i
    2021-01-31 22:58

    If you need to convert the value to a unix timestamp, the code to do so would be this:

    -- Assuming a date pattern like: yyyy-mm-dd hh:mm:ss
    local pattern = "(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)"
    local timeToConvert = "2011-01-01 01:30:33"
    local runyear, runmonth, runday, runhour, runminute, runseconds = timeToConvert:match(pattern)
    
    local convertedTimestamp = os.time({year = runyear, month = runmonth, day = runday, hour = runhour, min = runminute, sec = runseconds})
    

提交回复
热议问题