How do i add the st,nd,rd or th at the end of a date month?

so when using os.date I want it to print something like this : → August 25th the code I am using right now is :

print(os.date("%B %d", os.time()))

the problem with this is that it prints August 25 and not August 25th anybody know how to get the “st”, “nd”, “rd”, or “th” at the end of a month date

I haven’t really tried to do this before, but if I had a guess I’d say take the last digit and have a function that determines if that digit requires a “st”, “nd”, “th”, or “rd” at the end of it.

(For example, if you put 23 into the function it’d take the length of the number (convert it to a string) and if the last digit is a 3, it’d know to put “rd” at the end of it if that makes sense.)

Good luck!

1 Like

i mean yeah it works but is there a more efficient solution to this than just doing this

local str = "20"
local var = "th"
--
if #str == 2 then
    local sec = string.sub(str,2,2)
    if sec == "1" then
        var = "st"
    elseif sec == "2" then
        var = "nd"
    elseif sec == "3" then
        var = "rd"
    end
end
--
print(str..var)

I hope this post will help.
It shows how to get the last digit of a number and I you can then use conditionals to determine which (rd,th etc) is needed.

1 Like