As a Roblox developer it is currently too hard to format strings so they have date and time information in them. This prevents easy usage of the return of os.date. In vanilla Lua, there is an option in os.date that allows formatting of strings with time information using options similiar to strftime in C. This makes displaying dates and time easy and quick. If you wanted to display the current time for example, you could simply use os.date("%c")
. This returns 02/19/19 20:17:47
at the time of writing.
In Roblox’s version of os.date, this is not supported. If the first argument to os.date is anything but *t or !*t, it throws an error. This makes the above example far more tedious:
function DisplayDateTime()
local date = os.date("*t")
return string.format("%02d/%02d/%02d %02d:%02d:%02d", date.month, date.day, date.year%100, date.hour, date.min, date.sec)
end
While it is obviously possible to do it like this, it is annoying and can get long for complex formatting, and could lead to confusion from newer developers.
I noticed while going to answer this question that Roblox’s implementation of os.date does not include the formatting option that’s in normal Lua. This means that questions like this will come up because there’s no built-in way to format dates.
Knowing this, I set out to write a proper porting of os.date’s formatting that works like it does in vanilla Lua so that I could suggest it next time someone asked. This module got long enough that I thought it warranted an actual feature request instead since this really should have been supported from the beginning.
There was also extreme difficulty writing the days of the week into this module due to timezones, which are notoriously irritating to handle. This was an issue because I couldn’t easily get what day of the week a year started on, meaning I had to do the math myself. If formatting options were added, this issue would disappear entirely.
If the formatting options for os.date were added, it would negate the issue with formatting the results of it, and make code cleaner overall. It would also bring Roblox’s Lua more in line with vanilla Lua.