I have a script that converts the time in lighting, which is 24-hour EST time, to 12-hour EST time with AM and PM. The script works, but the problem is, when the minutes is under 10, it leaves out the zero and turns it into something else.
Example:
Lighting (24-hour) = 13:02
Game time (12-hour) = 1:2 PM
Script:
local currentValue = script.Parent
local serverScriptService = game:GetService("ServerScriptService")
local values = serverScriptService:FindFirstChild("Values")
local TimeIsAM = values:FindFirstChild("TimeIsAM")
local function Update(offset)
local date = os.date("!*t")
if TimeIsAM.Value == true then
currentValue.Value = string.format("%i:%i AM", date.hour + (offset), date.min)
else
currentValue.Value = string.format("%i:%i PM", date.hour - 12 + (offset), date.min)
end
end
while true do
Update(-4)
wait(1)
end
(The showing the time script is somewhere else, and works just fine)
You can use the format string “%._d”, replacing the underscore with how much you want to sort of “elongate” the number by, in your case it would be “%.2d”
local currentValue = script.Parent
local serverScriptService = game:GetService("ServerScriptService")
local values = serverScriptService:FindFirstChild("Values")
local TimeIsAM = values:FindFirstChild("TimeIsAM")
local function Update(offset)
local date = os.date("!*t")
if TimeIsAM.Value == true then
currentValue.Value = string.format("%i:%i AM", date.hour + (offset), date.min)
else
currentValue.Value = string.format("%i:%i PM", date.hour - 12 + (offset), + "0" + date.min)
end
end
while true do
Update(-4)
wait(1)
end
If you just copy and pasted their code then yes you would see 0. The hour returned from os.date is based on a 24-hour clock so 12:00 is 00:00. Just subtract 12 from the number if it’s 0 or greater than 12 and use the absolute value so negative 12 (from 0 - 12) just becomes 12.
Zero padding (%0xd where x represents the number of zeros to pad) is the correct way to fill in zeroes for a single-digit to get a proper HH:MM time format.