Hello there,
I've got a clock that tells the player what time it is. I've read the manual, but I can't seem to find where to change it from 24-hour format to 12-hour format.
Does such a property exist, or do I have to do a bunch of if-else statements?
Yeah. Just do the "if-else" route.
It should only take one if and an int, depending on how you're displaying the time, e.g.
DateTime *dt = DateTime.Now;
int 12Hour = dt.Hour;
if (12Hour > 12) 12Hour -= 12;
Display("The time is: %d:%02d:%02d", 12Hour, dt.Minute, dt.Second); // or lTime.Text = ..., or whatever
Minutes and seconds won't be any different, so you can still use them directly.
EDIT:
Oh, wait. You'll need something to display AM/PM as well, won't you? So, an if, an int and a String...
DateTime *dt = DateTime.Now;
int 12Hour = dt.Hour;
String ampm = "AM";
if (12Hour > 12) {
12Hour -= 12;
ampm = "PM";
}
Display("The time is: %d:%02d:%02d %s", 12Hour, dt.Minute, dt.Second, ampm); // or lTime.Text = ..., or whatever
Isn't 12:00:01 (one second after noon) already displayed as 12:01 PM?
(There's a dispute whether 12 PM should refer to midnight or noon, but I believe noon is common.)
Ugh, you're right. 12 noon SHOULD be PM, and isn't with that code. Also, 12 midnight is still displayed as 0. So yeah, a few more if ... elses needed.
if (dt.Hour >= 12) ampm = "PM";
if (12Hour > 12) 12Hour -= 12;
else if (12Hour == 0) 12Hour = 12;
That's what I get for trying to simplify the code too much. Stupid time, with all its stupid rules...
Thank you, Guy. I finally got around to trying this.
You're first code worked perfectly, and no, I'm not displaying AM or PM :)
Thank you!