Question

I have a question regarding date formatting in Lua (Luajit). I need to get UTC string, for example, like I would do it in JavaScript:

var date = new Date()
console.log(date.toUTCString()) // "Fri, 06 Dec 2013 14:05:28 GMT"

Unfortunately in Lua I cannot find possibility to format date this way:

print(os.date()) -- Fri Dec  6 16:06:43 2013
Was it helpful?

Solution

From the 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 function strftime.

Based on this and a little documentation referencing, it's quite simple to construct a format string that resembles JavaScript's toUTCString format.

> =os.date('!%a, %d %b %Y %H:%M:%S GMT')
Fri, 06 Dec 2013 14:27:34 GMT
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top