Hi, I’m needing some help with printing the current time and date but in a different format which I can’t figure out how to get. I need the format to be like this (image below) but I can’t find any posts on the DevForum with a solution that works. I also need the date to be formatted like this:
Date/Month/Year
2 Likes
Yoo could try using DateTime:FormatUniveralTime()
or the os
library, although I think DateTime
would be much more efficient. Hope this helps!
2 Likes
Hello @FireManGuy4321!
There is some documentation about this if you want to read it.
Here is a simple example:
local Date = os.date("*t") -- Change "*t" to "!*t" if you want the current time of the server (UTC I believe) instead of the local time of the player.
-- Get the date in 3 separate variables
local Month = Date.month
local Day = Date.day
local Year = Date.year
-- Get the time and format it
local Hour = (Date.hour) % 24
local AmOrPm = Hour < 12 and "AM" or "PM"
local Time = string.format("%02i:%02i %s", ((Hour - 1) % 12) + 1, Date.min, AmOrPm)
-- Defines and formats the current date and time into a single string
local DateAndTime = Day.."/"..Month.."/"..Year.." "..Time
print(DateAndTime) -- 08/08/2023 6:48 PM
Let me know if this works or not.
EDIT: I forgot to reference this, however, the formatting of the time was found on the DevForum here and I just adjusted it slightly.
3 Likes
This worked perfectly, it’s literally just what I needed. Thank you!
1 Like
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.