You can write your topic however you want, but you need to answer these questions:
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.
What is the issue? Include screenshots / videos if possible!
I don’t know how to do this!
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.
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
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
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.