- Home /
Conversion of seconds to hours and minutes
Hey guys, i am trying to make a timer that inclues hours and minutes, this timer goes up to 24hours and once it reaches that point it resets back to 0 and repeats. So basically a 24 hour cycle that allows me to decide how many seconds their are per day and converts those seconds to display hours and minutes. Currently my hours work fine and so do the days, but i can´t get the minutes to work, any guidance is appreciated. Here is my day/night cycle:
//Update Time
void ChangeTime()
{
//Increase Time
curTime += Time.deltaTime / secondsPerDay;
IncreaseHour();
if (curTime >= 1)
{
IncreaseDay();
curTime = 0;
}
}
void IncreaseHour()
{
float hour = curTime * 24f;
float minute = hour * 60;
if (minute >= 60)
{
minute = 0;
}
Debug.Log(minute);
ui.UpdateHour(hour);
ui.UpdateMinute(minute);
}
Thanks guys,
Answer by Bunny83 · Jun 15, 2019 at 04:22 PM
That's all:
float t = curTime * 24f;
float hours = Mathf.Floor(t);
t *= 60;
float minutes = Mathf.Floor(t%60);
t *= 60;
float seconds = Mathf.Floor(t%60);
Debug.Log(hours + ":" + minutes + ":" + seconds);
I see, it worked! Looks like ill have to research $$anonymous$$athf.Floor since i hadn't come across it yet. Thanks for the help!
The floor (and ceiling) function is a common mathematical concept. Though more important is the usage of the modulo operator %
Your answer
Follow this Question
Related Questions
How do you get the 24 Hour Time from rotation on the x axis? 1 Answer
C# Check If Minutes equal some denomination of 5 3 Answers
Multiple Cars not working 1 Answer
Realtime daylight cycle? 1 Answer
Distribute terrain in zones 3 Answers