How can I take a letter out of a string and convert it to 24 hour clock?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m making a game where you can look up a player and see info about them. To get most of the info, I’m sending HTTP requests to the certain Roblox API urls. With the last online date and time, it says the date, and then a “T” and then the time in 24-hour form. First of all, the T is very non-user-friendly, meaning that users wouldn’t understand what the T even means. Second, I don’t know how to read a 24 hour clock, and most people don’t either. So I want to somehow convert it to 12-hour clock with AM/PM at the end.

  2. What is the issue? Include screenshots / videos if possible!
    I don’t know how to do this!

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I could put each character of the string into a table and get the 11th character and then destroy it. But I don’t know how to do this.

To convert 24-hour time to 12-hour time, just subtract any 24-hour time after 12 (13-24) by 12 to get the time in PM.

Example:
17:00:00 in 24 hours time is 5:00:00 PM in 12 hours time.
17-12 = 5

1 Like

Thanks, this helps a lot! But you can’t subtract a time, so would I just string.split() at the first colon?

1 Like

Yep, you can do that and index it at 1 to subtract just the hour since minutes and seconds are the same in 24 and 12 hour clocks.

1 Like

Ok, figured out by myself how to take the letter out.

Solutions:

Thanks for the help!

For reference, please include all details when asking for a thread so that it’s easier to help you such as the time string you’re working with that you want to convert. I was able to figure it out by reading the context of the thread but it’d be easier if you supplied the exact time you got.

Roblox endpoints return time in ISO 8601 format. DateTime has a method to parse ISO 8601 into a format more readable for developers to work with called fromIsoDate. It is a 24-hour clock already so you simply need to pass in the time and then read off the returned data.

local dateInfo = DateTime.fromIsoDate(what_you_got_from_endpoint):ToUniversalTime()
local clockString = string.format("%02d:%02d:%02d", dateInfo.Hour, dateInfo.Minute, dateInfo.Second)

If you need to convert the hour to work with a 12-hour format then what you can do is:

  • Use modulus on the hour. This will try to divide the number equally into the given amount and return the remainder. Modulus into 12 will return 0 every time a number can be divided by 12 as it’ll have no remainder.
  • Check the division of the hour by 12. You will get a decimal value which you can essentially think of as “what % of 12 has occurred?”. If the coefficient is 1, it’s PM.
  • Clean up the hour so that if it’s 0 (24th hour), you set it back to 12.
local hour = dateInfo.Hour
local hour12h = math.floor(hour%12)
local isPM = (hour/12) >= 1

-- Correct Hour 0 (24H) to Hour 12 (12H)
if hour12h == 0 then hour12h = 12 end
1 Like

OH DUMB I kinda just took out the “T” with string.split, took off everything after the time with string.split, and then took the hour of the 24-hour time and converted it to a number and if it’s greater than or equal to 12 then add PM to the end but make sure to add the minutes

That didn’t work. I tried the ROBLOX account and they joined around 3PM, but your script is making it say they joined at 9 PM.

The ISO date for the Roblox account is 2006-02-27T21:06:40.3Z. T stands for time and separates the time value from the date. The first number after time is 21 which is (21 - 12 = 9) hours past 12. Any number higher than 12 represents a time in the afternoon. If you get 9 PM, it works as intended.

If the Roblox account joined at 3PM the ISO date would be 2006-02-27T15:06:40.3Z.

See: ISO 8601 - Wikipedia

2 Likes

What time zone is that though? Maybe we’re in different timezone. Or if ISO has a timezone

This is UTC. The Z represents the time offset which, as Z, has no offset from UTC.

2 Likes

image

image

THAT’S why.