Click here to Skip to main content
15,881,797 members
Articles / Desktop Programming / MFC
Article

Creating a Time Picker with no Seconds Field

Rate me:
Please Sign up or sign in to vote.
4.83/5 (12 votes)
29 Nov 19991 min read 75.6K   17   3

Recently I found myself needing a time picker that would show just the hours and minutes fields, without the seconds. Since the common control has no built-in style to do this, I did the logical thing and wrote a snippet of code to set a picker control to have such a format.

I also needed the code to work on any language and time format, so I delved into the SDK docs and read up on GetLocaleInfo(), which is the key to making the code work with all time formats.

The steps involved in this procedure are:

  1. Get the time separator (usually ':') and the current time format picture.
  2. Search for "ss" following the separator. This handles formats like "hh:mm:ss". If that substring is found, delete it from the format picture and jump to step 4.
  3. Search for "s" following the separator. This handles formats like "hh:mm:s". If that substring is found, delete it from the format picture.
  4. Set the time picker's format to the modified format we just created.

Note that this method doesn't care about the AM/PM indicator, if there is one. It will remain where it is in the format picture.

This code was written in MSVC 6 on Win 98. I use CString::Delete() (an MFC 6 function) to remove characters, so it will require a bit of tweaking to run on VC 5. It should work fine in Unicode, since I do everything with CStrings, although I haven't tested it.

TCHAR          szBuf[64];               // workspace
CString        sTimeFormat;             // the time format being worked on
CString        sSearch;                 // the string to search for
int            nIndex;                  // index of the string, if found
CDateTimeCtrl* pTimeCtl;                // pointer to a time picker

    // First get the time format picture.

    VERIFY ( ::GetLocaleInfo ( LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT,
                               szBuf, 64 ));

    sTimeFormat = szBuf;

    // Next get the separator character.

    VERIFY ( ::GetLocaleInfo ( LOCALE_USER_DEFAULT, LOCALE_STIME, 
                               szBuf, 64 ));

    // Search for ":ss".

    sSearch = szBuf;
    sSearch += _T("ss");

    nIndex = sTimeFormat.Find ( sSearch );

    if ( -1 != nIndex )
        {
        // Found it!  Remove it from the format picture.
        sTimeFormat.Delete ( nIndex, sSearch.GetLength() );
        }
    else
        {
        // No ":ss", so try ":s".

        sSearch = szBuf;
        sSearch += 's';

        nIndex = sTimeFormat.Find ( sSearch );
    
        if ( -1 != nIndex )
            {
            // Found it!  Remove it from the format picture.
            sTimeFormat.Delete ( nIndex, sSearch.GetLength() );
            }
        }

    // Now set the picker control's format.
    // NOTE: You'll need to set pTimeCtl somehow, such as with GetDlgItem().

    ASSERT_VALID(pTimeCtl);
    VERIFY ( pTimeCtl->SetFormat ( sTimeFormat ));

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) VMware
United States United States
Michael lives in sunny Mountain View, California. He started programming with an Apple //e in 4th grade, graduated from UCLA with a math degree in 1994, and immediately landed a job as a QA engineer at Symantec, working on the Norton AntiVirus team. He pretty much taught himself Windows and MFC programming, and in 1999 he designed and coded a new interface for Norton AntiVirus 2000.
Mike has been a a developer at Napster and at his own lil' startup, Zabersoft, a development company he co-founded with offices in Los Angeles and Odense, Denmark. Mike is now a senior engineer at VMware.

He also enjoys his hobbies of playing pinball, bike riding, photography, and Domion on Friday nights (current favorite combo: Village + double Pirate Ship). He would get his own snooker table too if they weren't so darn big! He is also sad that he's forgotten the languages he's studied: French, Mandarin Chinese, and Japanese.

Mike was a VC MVP from 2005 to 2009.

Comments and Discussions

 
GeneralThank you Pin
IVarshilov15-Jul-09 23:43
IVarshilov15-Jul-09 23:43 
GeneralPerfect! Pin
Stober19-Sep-05 10:43
Stober19-Sep-05 10:43 
GeneralNice but... Pin
Yogurt21-Nov-03 12:47
Yogurt21-Nov-03 12:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.