Adding time from IRL Times

Hello,
I would like to add 25 minutes from a IRL time, Would this be possible?

eg:

IRL time is 19:30
Time On Text = 19:55

2 Likes

idk if this is what you want

local dateObject = Date.now():ToUniversalTime()
script.Parent.Text = dateObject.Hour .. ":" .. dateObject.Minute + 25

also i dont know if it will take into consideration if it will go to the next hour or not, ex:
7:55 + 25 = 8:20
the script will prob look like this:
7:55 + 25 = 7:80

I don’t think that’s very helpful for the person since 7:80 isn’t valid time.

3 Likes

There is an old topic of mine that shows how to display time in a format that looks like this: Hours:Minutes:Seconds. Here it is:

If we take the function in the solution, we can convert any amount of time in seconds to a string that would look like a digital clock (Minutes:Hours:Seconds). Next, we need to somehow calculate what the current time is. Now this is fairly simple. We can use tick() with some extra math to calculate what the current time is. Now, tick is the amount of seconds since January 1 1970 (relative to the device’s time zone), instead the amount of time since midnight of the day before, so we have to do some extra stuff to this tick value before we can use it. Luckily this is fairly simple. We can use something called modulo. Now I don’t really know much about it, so don’t ask me how it works. Since there are 86400 seconds in a day, we can convert our simple little tick() into something like this tick() % 86400. Now I’m not sure how the math behind module works, but essentially what is happening is every time tick is larger than 86400, it will loop back around to zero plus the amount higher than 86400 that it was (tick() - 86400), and it will do this until tick() is between zero (0), and 86400. This should give you the time that it is in real life. Now we can add any number (in the unit of seconds) to this new tick value, and we will be adding time to the time in real life. We can plug this value into the function from the solution to my (rather old) topic. Note that, you should perform the modulo after you perform the adition of the tick, and the time you want to add ((tick() + Seconds_I_Want_To_Add)%86400). Here is what it should look like in code:

local extraTimeInSeconds = 25
local currentTime = tick()
local currentTimeAdded = (currentTime + extraTimeInSeconds)%86400
function HoursConversion(tim)
	return("%02i:%02i:%02i"):format(tim/3600, tim/60%60, tim%60)
end
HoursConversion(currentTimeAdded)

Now this will return what is known as “Military Time”. And yes, believe it or not… there is a time system that is called “Military Time” out there that doesn’t loop the hours from twelve a clock, back to one, and instead goes all the way up to twenty four hours, and then loops back down to zero (I think) or one or something like that. Don’t ask me why it is used, or why it even exists. I have no idea why. Please.

But we can fix this, by changing my function from what it is now, to this:

function HoursConversion(tim)
	return("%02i:%02i:%02i"):format((tim/3600)%12 + 1, tim/60%60, tim%60)
end

What the function is doing now, is it’s dividing tim by 3600, and then it’s looping the tim / 3600 value back to zero (0) if it’s more than twelve, and then adding the difference (tim - 12), and it keeps doing this until it’s between 1, and 12. And then it adds one to all of that ((tim / 3600) % 12 + 1). The reason why we divide by 3600 (which seems like an arbitrary / random number), is because there are 3600 seconds in an hour.

Now, our final code looks like this, and it looks very nice:

local function HoursConversion(tim)
	return("%02i:%02i:%02i"):format((tim/3600)%12 + 1, tim/60%60, tim%60)
end

local function addIrlTimes(secondsToAdd)
    local addedTime = (tick() + secondsToAdd)%86400
    return HoursConversion(addedTime)
end

print("In 25 seconds the time will be equal to: "..addIrlTimes(25).." !")

I tested it, and it works… really well.

Now, just in case you are wondering… Here is the documentation for the tick() function.

Edit: Oh, and to add minutes instead of seconds, you can convert seconds to minutes. So when you run the function (addIrlTimes), you can multiply the argument that you use as seconds secondsToAdd by sixty (60), and you will now do this instead:

local function addIrlTimes(minutes)
    local minutesToAdd = minutes * 60
    local addedTime = (tick() + minutesToAdd)%86400
    return HoursConversion(addedTime)
end

You can also do this, if you want to add hours instead of minutes or seconds:

local function addIrlTimes(hours)
    local hoursToAdd = hours * 60 * 60
    local addedTime = (tick() + hoursToAdd)%86400
    return HoursConversion(addedTime)
end

(You just have to replace the original addIrlTimes function, with one of the new addIrlTimes function in this edit)

Hello, How can I edit this to minutes and seconds

If you just want minutes and seconds you need to edit the HoursConversion function like this:

function HoursConversion(tim)
	return("%02i:%02i"):format(tim/60%60, tim%60)
end

Hello, I’m having the issue of it turning into 12 Hour Time instead of 24 hours. How can I fix this?

12:50 + 20 = 01:20 - Issue

I need it to be 12:50 + 20 = 13:20

How can I fix this?