How would I go about making roblox's "Military time" to normal time

So I have it set so the clock gui is only running the normal lighting time “14” and stuff like that, but I would like to do be regular, how would I do this?

1 Like

If the time is past 12:00:00 subtract 12 hours. If you’re feeling spicy you could even add an am or pm.

3 Likes

I have this.

while true do

wait(.5)

script.Parent.TextLabel.Text = game.Lighting.TimeOfDay

wait(.1)

end

Military time is actually 24-hour time, where numbers are displayed between 0 and 24. What you’re looking for is to convert this into 12-hour time, which the hour is between 12 with an ante/post meridiem (AM/PM) mark to show the hours before or past midday, 12:00.

You’ll need to do a few conversions here, based on the number itself. Don’t overwrite the number however, since you’ll need it for later. The first is to get the hour between 0 and 12, then to determine whether it is ante or post meridiem.

Here is some code that will accomplish that. I will then explain it afterward.

local hourRaw = 13
local hour12h = math.floor(hourRaw % 12)
local isPM = (hourRaw / 12) >= 1

if hour12h == 0 then
    hour12h = 12
end

Within this code, we get the hour, whatever that may be. Remember, we need to focus on converting the hours, not the minutes - those don’t matter here. In my sample, I use a variable assigned to 13 to get my hours. In your case, you best use TimeOfDay over ClockTime to get hours due to its accuracy. Considering it’s a string, we need to convert it into a usable number.

local Lighting = game:GetService("Lighting")
local Hour = tonumber(Lighting.TimeOfDay:match("^%d+"))

The above code looks at TimeOfDay, then performs a string operation, match. Once the string is matched to the specified case, it turns that into a number. The character case I used is ^%d+, which I will break down for you.

  • ^ anchors the match to the beginning of the string.
  • %d fetches the first match of any digit.
  • + asks the operator to go until the proceeding character is not a digit.
  • tonumber turns this all into a number. Since there are no alphabetical characters, this will give you the hour in a usable number.

Now that we have our hours (which would be assigned to hoursRaw), we perform two math operations on it. The first is to use the modulus operator, %. I personally don’t know the mathematical concept behind it but I know how it works. Think of it like this: the second number is subtracted from the first unless the first number is greater than the second. Modulus is actually a divisor though.

local hour12h = math.floor(hourRaw % 12)

if hour12h == 0 then
    hour12h = 12
end

We use the following to determine the hour in 12-hour time. Remember that in 24-hour time, 12 and 24 both represent halves of a day. Therefore, calling modulus on each of these is 0. We don’t have a 0:00 in 12-Hour time, so we change this back to 12.

And finally, converting the hours to 12-hour time is out of the way, so now we need to determine whether it is past midday (12:00) or not. This is a simple math concept of checking if a number is above or below a point. We just divide and check if the number is greater than or equal to one to see if it’s PM.

local isPM = (hour / 12) >= 1

Then, all you have left to do is format your string based on the math operations you performed. Huzzah, what a piece of work you have now! Based on the code you recently posted, you can perform a string operation that replaces certain patterns with variables and you’ve got yourself something nice.

Getting minutes is something you’ll have to work out on your own using string manipulation. At the very least though, keep in mind we have access to string.split now. You can split TimeOfDay by each : and use the entries in the return array [1] for hours, [2] for minutes and [3] for seconds.

local formattedTime = ("%d:%d %s"):format(hour12h, minutes, isPM and "PM" or "AM")

Salvage at will! Have fun!


@wafflecow321

Good call. I’ve got some code to back you up here:

local hour12h = (hour > 12 and hour or hour - 12)

if hour12h == 0 then
    hour12h = 12
end

For a moment, I misread your post and thought you were doing simple-subtraction of 12 from the number of hours, but I saw that you said specifically to do so when it’s past 12. The previous content I put down was wrong, sorry about that.

2 Likes

So I read all this and am confused, which part would be the full code to help my code I provided?

Which part confuses you? I thought I explained it pretty thoroughly. The very first code sample I posted shows how to convert to 12-Hour time and the rest is an explanation of how that code works.

I did not provide any full code, as the Scripting Support category is not for spoonfeeding code. It is up to you to determine how to put this into your code to help you.

On the other hand, if you’re confused at where things go, I can clarify it for you.

local Lighting = game:GetService("Lighting")

local hourRaw = tonumber(Lighting.TimeOfDay:match("^%d+"))
local hour12h = math.floor(hourRaw % 12)
local isPM = (hour / 12) >= 1

if hour12h == 0 then
    hour12h = 12
end

This is the conversion code. Weaving it into your code is up to you.

1 Like