Click here to Skip to main content
Click here to Skip to main content

Creating a Time Picker with no Seconds Field

By , 29 Nov 1999
Rate this:
Please Sign up or sign in to vote.
<!-- Article Starts -->

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

About the Author

Michael Dunn
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 PinmemberIVarshilov15-Jul-09 23:43 
GeneralPerfect! PinmemberStober19-Sep-05 10:43 
GeneralNice but... PinmemberYogurt21-Nov-03 12:47 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 30 Nov 1999
Article Copyright 1999 by Michael Dunn
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid