How to make 7:9 PM go to 7:09 PM

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)

If a number is less than 10, add a 0 Infront of that number.

local extra_zero = if seconds < 10 then "0" else ""
print(minutes.. ":".. extra_zero.. seconds)

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”

print(string.format('%.2d', 9)) --> 0.9
print(string.format('%.2d', 10)) --> 10

End result would be something like:

string.format('%1:%.2d', date.hour + offset, date.min)

try this :

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

After doing that, the hours (its 12:49AM EST in this photo) get converted to 0 or 00
image

Just make a check, that if the converted number is == 0, then change it to 12? (the front number that is)

And then check if the number changes from 12 to 13, then change 13 into 1. Then it should loop like you want it.

There may be a smarter way to do it.

That’s not the problem, the problem is, the minutes side won’t show the 0 if it is in the first place

image
So if a 0 is present here, it wont show you mean?

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.

2 Likes