• We should now be fully online following an overnight outage. Apologies for any inconvenience, we do not expect there to be any further issues.

C++ Question => Converting Military Time to Regular Time?

CoBRaXT

Golden Member
Mar 11, 2002
1,241
0
76
A user inputs military time (eg 2305) and the program has to convert it to 11:05 P.M.
I know it has to do with modulus division but I'm not quite sure how to set it up.

Thanks
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
hour %= 12;

although that would display 0 for 12xx

so just use an if statement

 

brtspears2

Diamond Member
Nov 16, 2000
8,659
1
81
I would be lame, if statement to get am or pm, and do 2300-1200 to get normal time, and a if statment to handle 0 hours.
 

dmurray14

Golden Member
Feb 21, 2003
1,780
0
0
I don't know how you do this in C, but just say if the number is greater than 12 subtract 12.
 

Fiveohhh

Diamond Member
Jan 18, 2002
3,776
0
0
Originally posted by: brtspears2
I would be lame, if statement to get am or pm, and do 2300-1200 to get normal time, and a if statment to handle 0 hours.

me 2
 

KingNothing

Diamond Member
Apr 6, 2002
7,141
1
0
Originally posted by: NogginBoink
I wouldn't use modulus division.

I'd compare it to 1259 and if greater, subtract 1200.

Assuming the input is entered as 2305, not 23:05, that is the best method.
 

KingNothing

Diamond Member
Apr 6, 2002
7,141
1
0
Originally posted by: Ameesh
omg you are soo gonna fail your class if you cant even get this done.

Well sure, but would you rather make fun of him for this small thing now, or make fun of him when he posts that he's failed his class later?
 

NogginBoink

Diamond Member
Feb 17, 2002
5,322
0
0
Originally posted by: KingNothing
Originally posted by: Ameesh
omg you are soo gonna fail your class if you cant even get this done.

Well sure, but would you rather make fun of him for this small thing now, or make fun of him when he posts that he's failed his class later?

LOL! That's so wrong... yet so right!
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: KingNothing
Originally posted by: Ameesh
omg you are soo gonna fail your class if you cant even get this done.

Well sure, but would you rather make fun of him for this small thing now, or make fun of him when he posts that he's failed his class later?

you have a point.
 

FeathersMcGraw

Diamond Member
Oct 17, 2001
4,041
1
0
Originally posted by: KingNothing

Well sure, but would you rather make fun of him for this small thing now, or make fun of him when he posts that he's failed his class later?

This is not an either/or scenario.
 

Rogue

Banned
Jan 28, 2000
5,774
0
0
And to you all I present the following question:

WHAT THE HELL IS WRONG WITH MILITARY TIME?

I have everything around me set to 24HR time rather than that lame ass 12HR stuff. Military time owns you.
 

Mday

Lifer
Oct 14, 1999
18,647
1
81
assuming you read the 2305 as a string... convert it into a number. then if that number is greater than 1200, subtract 1200, and have a boolean value for AM or PM. mod by 100 to get the first 2 digits, div by 100 to get 2nd two digits, and then print them out.

or you can use strings to store that then output the string.
 

RagingBITCH

Lifer
Sep 27, 2003
17,618
2
76
Originally posted by: Rogue
And to you all I present the following question:

WHAT THE HELL IS WRONG WITH MILITARY TIME?

I have everything around me set to 24HR time rather than that lame ass 12HR stuff. Military time owns you.

You're making the incorrect assumption that everyone here has a problem with military time. Jeez, lighten up.