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

Thread: 12 hour increments instead of 24 hour

  1. #1

    12 hour increments instead of 24 hour

    How would I switch from 24 hour increments instead of 12 hour using CTime.
    My creation is my reality.

  2. #2
    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. #3
    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);


    }
    My creation is my reality.

  4. #4
    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.
    My creation is my reality.

  5. #5
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097
    Or ...

    Code:
      int h = GetHour() % 12;
    No compare or conditional....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center