Add weekday to DateTime

As a Roblox developer, it is currently too hard to derive the weekday from DateTime (i.e. a number representing the day of the week, such as Monday). The DateTime object returns info such as month, day, hour, etc., but it does not return info on the current day of the week.

If Roblox is able to address this issue, it would improve my development experience because I can create systems that can check on the day of the week easily without having to programmatically calculate the day.

Most date-time libraries include this out of the box. For example, JavaScript’s Date API has a getDay() method to get the weekday.


This could look like the following:

local now = DateTime.now()
print(now:ToUniversalTime()) --[[
{
  Year = 2022,
  Month = 2,
  Weekday = 5, -- ADDED HERE (5 representing Thursday)
  Day = 3,
  Hour = 9,
  Second = 0,
  Millisecond = 0,
}
]]

If anyone is trying to calculate it themselves, here’s code to do it:

local dt = DateTime.now()
local weekday = (math.floor(dt.UnixTimestamp / 86400) + 4) % 7 + 1
-- weekday = 1 thru 7, starting on Sunday
34 Likes

Super appreciate the code sample for calculating the day of the week. I asked for this along with a couple of other extra identifiers in DateTime. It’s really limited right now and doesn’t have as many identifiers as os.time but the API is functionally better and more intuitive than os.time especially in terms of control (os.time with a format string is forced to the machine’s time zone).

Plugs:

6 Likes

As a work around, have you tried using luadate?

1 Like