How do I make a 12 hour time gui?

I have a day night script and I want to make a time GUI which shows the in-game Roblox time with the 12 hour time with Pma nd AM. I do not know how to make it but I di make a 24 hour one and I want it to be 12 hours one.

3 Likes

It’s a simple case of checking what time it is and correcting accordingly:

if Time < 12 then
    Time = Time .. "AM" -- .. combines strings
else
    Time = Time - 12 .. "PM"
end

Lua automatically handles string => number and vise versa, so it works without tostring.
Just input your 24h time into this and it will give you a 12 hour time.

My current script is a local script inside the text label

while true do
script.Parent.Text = string.sub(game.Lighting.TimeOfDay,1,5)
wait()
end

I got interested in solving this. I came up with this:

function getLightingTime()
  local totalMinutes = game.Lighting:GetMinutesAfterMidnight()
  local hours = math.floor(totalMinutes / 60)
  local minutes = math.floor(totalMinutes % 60)
  local period

  if hours < 12 then
    period = "AM"
  else
    period = "PM"
    hours -= 12
  end
  
  if hours == 0 then
    hours = 12
  end
  
  return string.format("%02d:%02d %s", hours, minutes, period)
end

You should be able to plug this function into your previous code:

while true do
  script.Parent.Text = getLightingTime()
  wait()
end

Be warned that my code discards seconds completely. This is also what your original code did, so I’ll assume you don’t mind. A while loop here could be considered inefficient. I’ll let you worry about that :slight_smile:

6 Likes