Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How To Convert Army Time To Standard Time?

Asked by 5 years ago
Edited 5 years ago

Hello! I discovered this code on the website and I am wanting to know how can I make it so that it is in standard 12-hour time rather than the 24-hour army time. I am not sure how to go about this due to the fact that the ClockTime property of Lighting is in army time. Thank you!

local Lighting = game.Lighting

game.Lighting.Changed:connect(function()
    script.Parent.TextLabel.Text = Lighting.TimeOfDay:sub(1, 5)
end)
0
You shouldn’t use a while loop, instead use a changed event. User#19524 175 — 5y
0
Thank you. I have since then updated the code. BunnyFilms1 297 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

Since the hours only matter, you can take the minutes out of the equation with ClockTime, and make sure you round it down:

local hours = math.floor(Lighting.ClockTime)

To get our minutes we can use the substring you used, but with just the minutes. This can be a string because we're not doing anything to it:

local minutes = Lighting.TimeOfDay:sub(3,5)

Now we need to know whether it's AM or PM, and the best way to do this is see if hours is above 12. If it is above 12, it's PM. If it isn't, it's AM. Also, I added a space so it's easier to put together down the road:

local meridiem = hours >= 12 and " PM" or " AM"

Then we can use a nifty little operator called the Modulus (or %). If you take a modulus of two numbers, it gives the remainder as if they were divided. For example:

100 (%) 10 = 0

12 (%) 10 = 2

10 (%) 3 = 1

3 (%) 4 = 3

So using this we can take the hours and modulate it by 12:

local displayHours = hours % 12

Now since 0 % 12 = 0 and 12 % 12 = 0, we're gonna need to fix that because there's no Zero'O'clock:

if displayHours == 0 then displayHours = 12 end

Then we just paste the string together!

local finalTime = tostring(displayHours) .. minutes .. meridiem

Here's the whole code combined with yours:

local Lighting = game.Lighting
while true do

    local hours = math.floor(Lighting.ClockTime)
    local minutes = Lighting.TimeOfDay:sub(3,5)

    local meridiem = hours >= 12 and " PM" or " AM"
    local displayHours = hours % 12
    if displayHours == 0 then displayHours = 12 end

    local finalTime = tostring(displayHours) .. minutes .. meridiem

    script.Parent.TextLabel.Text = finalTime

    wait(0.1)
end

EDITS: Clarification and formatting

0
Works without error. Thank you for the answer! BunnyFilms1 297 — 5y
Ad

Answer this question