12 hour increments instead of 24 hour
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

Thread: 12 hour increments instead of 24 hour

1. Member
Join Date
Jan 2002
Posts
33

12 hour increments instead of 24 hour

How would I switch from 24 hour increments instead of 12 hour using CTime.

2. Member
Join Date
Apr 2002
Posts
163
If you're talking about displaying a CTime object as, say 3 PM or 11 AM, it's really easy.

// let's pretend theTime is 21:43 in 24 your format...
CTime theTime = CTime::GetCurrenTime();

CString strTheTime = theTime.Format("%I:%M %p");

// strTheTime now reads 9:43 pm

Voila!

To learn all about formatting time strings, look into strftime and wcsftime in the documentation...

3. Member
Join Date
Jan 2002
Posts
33
Actually I am trying to get an analog clock to read the time in 12 hour increments. Here is the code for the analog clock.

CTime t = CTime::GetCurrentTime();

//Create the hour hand
CPen Hour(PS_SOLID, 5, RGB(0, 0, 0));

CPen* pOldPen;
pOldPen=pdc->SelectObject(&Hour);

double HourValue=
((double)(t.GetHour()<=12?
t.GetHour():t.GetHour()-12)+
((double)t.GetMinute()/60.0)+
((double)t.GetSecond()/3600.0))*
((2 * 3.14)/12.0);
pdc->MoveTo(nCenterX,nCenterY);
pdc->LineTo(nCenterX + (int)((double)((nXRadius *50)/100)*
sin(HourValue)),
nCenterY - (int)((double)((nYRadius * 50)/100)*
cos(HourValue)));

//Create the minute hand
CPen Minute(PS_SOLID, 5, RGB(0, 255, 0));

pOldPen=pdc->SelectObject(&Minute);

double MinuteValue=
((double)(t.GetMinute()<=12?
t.GetMinute():t.GetMinute()-12)+
((double)t.GetHour()/24.0)+
((double)t.GetSecond()/3600.0))*
((2 * 3.14)/12.0);
pdc->MoveTo(nCenterX,nCenterY);
pdc->LineTo(nCenterX + (int)((double)((nXRadius *75)/100)*
sin(MinuteValue)),
nCenterY - (int)((double)((nYRadius * 75)/100)*
cos(MinuteValue)));

//Create the second hand
CPen Second(PS_SOLID, 5, RGB(255, 0, 0));

pOldPen=pdc->SelectObject(&Second);

double SecondValue=
((double)(t.GetSecond()<=12?
t.GetSecond():t.GetSecond()-12)+
((double)t.GetMinute()/60.0)+
((double)t.GetHour()/24.0))*
((2 * 3.14)/12.0);
pdc->MoveTo(nCenterX,nCenterY);
pdc->LineTo(nCenterX + (int)((double)((nXRadius *75)/100)*
sin(SecondValue)),
nCenterY - (int)((double)((nYRadius * 75)/100)*
cos(SecondValue)));
pdc->SelectObject(pOldFont);

}

4. Member
Join Date
Jan 2002
Posts
33
LOL! got it.
I forgot to change what I divided the clock by for the minutes and seconds. Dang copy and paste gets me every time.
Thanks.

5. Or ...

Code:
`  int h = GetHour() % 12;`
No compare or conditional....

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width