Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String Lua如何重新格式化日期?_String_Date_Lua_Format - Fatal编程技术网

String Lua如何重新格式化日期?

String Lua如何重新格式化日期?,string,date,lua,format,String,Date,Lua,Format,我目前正在检索格式为2020-09-23T09:03:46.242Z(YYYY-MM-DDThh:MM:ss.sssZ)的日期,并尝试将其转换为Wed Sep 23 09:03:46 2020。和弦的操纵做斗争,有人有什么想法吗? 基本上,我的目标是能够在日期上执行os.time(),但我知道我可能需要事先进行一些重新格式化 非常感谢您的帮助 谢谢,斯科特。试试这个: local s = '2020-09-23T09:03:46.242Z' local t = {} t.year, t.mont

我目前正在检索格式为2020-09-23T09:03:46.242Z(YYYY-MM-DDThh:MM:ss.sssZ)的日期,并尝试将其转换为Wed Sep 23 09:03:46 2020。和弦的操纵做斗争,有人有什么想法吗? 基本上,我的目标是能够在日期上执行os.time(),但我知道我可能需要事先进行一些重新格式化

非常感谢您的帮助

谢谢,斯科特。

试试这个:

local s = '2020-09-23T09:03:46.242Z'
local t = {}
t.year, t.month, t.day, t.hour, t.min, t.sec =
   assert(s:match'^(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)')
print(os.date('%c', os.time(t)))
local function convert (s)
    local source_format = '(%d%d%d%d)-(%d%d)-(%d%d)T(%d%d):(%d%d):(%d%d)%.'
    local year, month, day, hour, min, sec = string.match( s, source_format )
    local unix_time = os.time {
        year    = tonumber(year),
        month   = tonumber(month),
        day     = tonumber(day),
        hour    = tonumber(hour),
        min     = tonumber(min),
        sec     = tonumber(sec)
    }
    local target_format = '%a %b %d %H:%M:%S %Y'
    return os.date( target_format, unix_time )
end
试试这个:

local function convert (s)
    local source_format = '(%d%d%d%d)-(%d%d)-(%d%d)T(%d%d):(%d%d):(%d%d)%.'
    local year, month, day, hour, min, sec = string.match( s, source_format )
    local unix_time = os.time {
        year    = tonumber(year),
        month   = tonumber(month),
        day     = tonumber(day),
        hour    = tonumber(hour),
        min     = tonumber(min),
        sec     = tonumber(sec)
    }
    local target_format = '%a %b %d %H:%M:%S %Y'
    return os.date( target_format, unix_time )
end

是否要日期验证?是否要日期验证?