我对 Lua (Luajit) 中的日期格式有疑问。例如,我需要获取 UTC 字符串,就像我在 JavaScript 中那样:
var date = new Date()
console.log(date.toUTCString()) // "Fri, 06 Dec 2013 14:05:28 GMT"
不幸的是,在 Lua 中我找不到以这种方式格式化日期的可能性:
print(os.date()) -- Fri Dec 6 16:06:43 2013
最佳答案
来自 Lua manual :
If format starts with '
!
', then the date is formatted in Coordinated Universal Time. [...]If format is not "
*t
", then date returns the date as a string, formatted according to the same rules as the ANSI C functionstrftime
.
基于此和一点documentation referencing ,构造类似于 JavaScript 的 toUTCString
格式的格式字符串非常简单。
> =os.date('!%a, %d %b %Y %H:%M:%S GMT')
Fri, 06 Dec 2013 14:27:34 GMT
关于Lua中的日期格式toUTCString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20425867/