Displaying time in 12-hour format (SOLVED)

Started by Snake, Mon 15/06/2009 14:27:52

Previous topic - Next topic

Snake

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?
Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

Gilbert

Yeah. Just do the "if-else" route.

GuyAwesome

#2
It should only take one if and an int, depending on how you're displaying the time, e.g.
Code: ags

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...
Code: ags

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


Khris

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.)

GuyAwesome

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.

Code: ags

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...

Snake

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!

Grim: "You're making me want to quit smoking... stop it!;)"
miguel: "I second Grim, stop this nonsense! I love my cigarettes!"

SMF spam blocked by CleanTalk