I am having problem in converting military time to standard.

The task I have is to take time from different cities around the world, adjust it to eastern time, and then display it in standard format with AM or PM.

For example:

User gave time for london 23:40
I scan the hour scanf("%d", &londontime);

then I adjust it to eastern time

adjustedtime = 23 - 5;

now its 18:40 est
which should be 6:40 EST in standard form

what should be the formula to calculate it?

after that I cant figure out how to put it in standard form and figure out if its AM or PM. I am aware its a simple task but my brain isnt fully functioning right now. And I need to get this done asap, and submit it.

would appreciate some help.

Recommended Answers

All 4 Replies

Initially assume its AM.
To convert: if the hour is greater then or equal to 12 just subtract 12 and make it PM

11:30 is still 11:30 AM
12:30 becomes 0:30 PM
18:30 becomes 6:30 PM

Thanks it worked. :)
I need to get some sleep now.

One problem though. I think I'm doing something wrong here...I have

if (hour < 12)
{
printf("%d:%d AM ", hour, minute);
}
else
{
printf("%d:%d PM", (hour - 12), minute);
}

when I input 01:59 london time, I get -4:59AM?
anything wrong with the code

nothing wrong with the code you posted, but wrong with the conversion from London time to your time. Its not quite that simple -- if the result of the substraction is negative then add 12 hours, such as -4 + 12 = 8 PM. To keep the code you already posted you could then add another 12 to put the result into military time -- 8 + 12 - 20

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.