Getting 24 hour time

local function getTime(setTime)
	local Hours = tonumber(string.sub(setTime, 1, 2))
	local Minutes = string.sub(setTime, 4, 5)
	
	if setTime >= 13 and setTime ~= 0 then
		return string.format('%s:%s %s', tonumber(Hours) - 12, Minutes, 'pm')
	elseif setTime >= 12 and setTime < 13 then
		return string.format('%s:%s %s', Hours, Minutes, 'pm')
	elseif setTime >= 0 and setTime < 1 then
		return string.format('12:%s %s', Minutes, 'am')
	else
		return string.format('%s:%s %s', Hours, Minutes, 'am')
	end
end

local StartTime, EndTime = getTime(JobInfo.StartTime), getTime(JobInfo.EndTime)
		
Time.Text = StartTime .. ' - ' .. EndTime

Kind of a lil bugged. Instead of being like 9:00am - 3:00pm it just has this

2 Likes

return string.format(‘%d:%02d %s’,

%02d will specify that it must include at least two digits. You can do likewise for the hours if you want 09:30.

You’re using the wrong format specifier, %s is for strings, and you could do the formatting in a single statement:

local function formatAsDigital(input)
    local hours = math.floor(input/60);
    return string.format("%d:%02d %s", hours%12, input%60, hours >= 12
and "pm" or "am");
end

The above reply explains the format specifier well, but it can be any digit

string.format("%05d", 9) for example, would result in 00009, since 9 isn’t 5 digits, so the first 4 are just a “placeholder”

Not sure how you want it, but how this works is it treats the argument received as minutes, so if you pass 60, you get "1:00" in return. If that’s not what you want lmk and i’ll make the changes necessary

Huh ok, what would input be? Just the time of day??

EDIT Nvm, that didn’t work, had this:

local function getTime(setTime)
	local hours = math.floor(setTime/60)

    return string.format("%d:%02d %s", hours%12, setTime%60, hours > 12 and "pm" or "am")
end

local StartTime, EndTime = getTime(JobInfo.StartTime), getTime(JobInfo.EndTime)
		
Time.Text = StartTime .. ' - ' .. EndTime

and got 0:09am - 0.14am

input is treated as minutes (can also be seconds, since it works exactly the same, just gotta multiply or divide) and what is JobInfo.StartTime and EndTime?

My function takes a number, your original function took a string, judging from the use of string.sub

formatAsDigital(120) == "2:00 am"

StartTime is 9 and EndTime is 14 (being the 24 hour times for 9am and 2pm)

Yeah that’s because input was treated as minutes.

Multiply it by 60, and it will work

Hmm, minor problem I get, is if my time == 12 (for 12 pm) it gets set to 0:00 am instead of 12:00 pm

hour%12 + 1

30 character bypass

1 Like

Sorry for the late reply, but that doesn’t really fix it. It just adds 1 hour to the time.

Lighting:GetPropertyChangedSignal('ClockTime'):Connect(function()
	local NewTime = Lighting.ClockTime * 60
	local Hours = math.floor(NewTime / 60)

    Time.Text = string.format('%d:%02d %s', Hours%12 + 1, NewTime%60, Hours > 12 and 'pm' or 'am')
end)

So it’s saying midday is 1:00pm instead of 12:00pm

Separate the operation. Instead of adding one to the hours, perform modulus in a variable and set it up to 12 if it’s 0.

Lighting:GetPropertyChangedSignal('ClockTime'):Connect(function()
	local NewTime = Lighting.ClockTime * 60
	local Hours = math.floor(NewTime / 60)
    local HoursConverted = Hours % 12

    if HoursConverted == 0 then
        HoursConverted = 12
    end

    Time.Text = string.format('%d:%02d %s', HoursConverted, NewTime%60, Hours > 12 and 'pm' or 'am')
end)