Simple Clock (AM/PM, not Military)

I’m trying to make a simple clock. This should be easy right? I don’t know why it won’t work for me. Here’s the code I have so far.

    void HandleClock()
    {
        guiTime = Time.time;

        if (minute >= 59)
        {
            hour += 1;
        }
        if(hour == 0)
        {
            hour = 12;
        }
        else if(hour >= 13)
        {
            hour = 1;
        }
        minute = (guiTime * 10) % 59;

        clock = string.Format("{0:00}:{1:00}", hour, minute);
        
        if (hour != 12)
            doOnce = false;

        if (!doOnce)
        {
            if (hour == 12  ampm == "am")
                ampm = "pm";
            else if (hour == 12  ampm == "pm")
                ampm = "am";
            doOnce = true;
        }
    }

The minutes get to 59, but the hour won’t change. If I change the if statement to >= 58, the hour goes up 6 times. I could do some kind of ghetto rig to get that to work, but that seems counter-productive and I feel like I should learn it the correct way.

I googled how to do a clock and also looked up in the unity forums how to do it, but found nothing. All I find are countdown timer samples, which helped me at least get to this point. I hope someone can point me in the right direction here. I’m sure it’s something simple, but I don’t know what it is.

Thanks!

maybe check DateTime class
http://www.dotnetperls.com/datetime-format

I thought there was some sort of command for time… I just couldn’t find it anywhere. Thanks

EDIT: So apparently DateTime doesn’t work. I don’t want it to be real life time. I’m trying to make an in game clock. Can DateTime still manage this and I’m just missing it?

I figured out how to make a date using guiTime = new DateTime(2012, 1, 1, 7, 0, 0);, but I can’t seem to get it to add minutes or hours to it. I put guiTime.AddHours(5); and it does nothing. Same for minutes.

EDIT: Figured it out. I was putting guiTime.AddMinutes(#); to add stuff. I had to do guiTime = guiTime.AddMinutes(#)… Woops

Hello,

I don’t see why you can’t get it working with the Unity code. I’d imagine that your problem is that the ‘minute’ variable is getting reset to zero before the hour increments. If I were you, I’d just forget about setting the hours based on minutes, and set the hours based on guiTime as well. For example: (Written in browser - may not compile :wink: )

void HandleClock()
    {
        guiTime = Time.time; //set guiTime
        second = Mathf.Round(Mathf.Repeat(guiTime, 60));  //use Mathf.Repeat to make the seconds reset to 0 when they get to 60
        minute = Mathf.Floor(Mathf.Repeat(guiTime / 60, 60));  //one minute = 60 seconds - once again, reset minutes to 0 when they get to 60
	hour = Mathf.Repeat(Mathf.Floor(guiTime / 3600), 13);  //this one's trickier - one hour = (60 * 60 seconds), and hours need to reset to 0 when they hit twelve

        if((Mathf.Floor(guiTime / 47000) % 2) == 0) {  //13 hours = 47000 seconds. guiTime / 47000 is even, then we need AM, otherwise, PM
              ampm = "am";
        } else {
              ampm = "pm";
        }

        clock = String.Format("{0:00}:{1:00}:{2:00} {3}", hour, minute, second, ampm);  //final clock string
}

Besides being cleaner/easier to read (in my humble opinion), the hours, minutes, and seconds are independent of each other so you can kill one without killing the others.
Hope this helps.

-Lincoln Green