Add formatting to os.date

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.

23 Likes

Sorry for bumping however I am desperately needing this! (I didn’t find this topic when I searched the first time, so I gave it a second shot, and I found this!)

As a Roblox developer it is extremely difficult to display a high-level representation of a given time. Roblox modifies os.date to return a table, however the vanilla behaviour is that a nicely formatted string is returned.

It can also take format specifiers, for example os.date("%x") returns a string of the date in mm/dd/yy format.

There are some use cases for this, such as

  • A countdown until an event
  • An in-game calendar

If this feature is added it would improve my experience because I don’t have to resort to doing something like

local date_info = os.date("*t") -- or maybe "!*t" if wanted
local readable_string = string.format("%d/%d/%d", date_info.month, date_info.day, date_info.year)

Then I would have to do more work if I wanted to display the seconds, minutes, hours, or even the week day. So a format specifier would be much cleaner.

1 Like