Use the Date/Time Picker common control (IE3+)

NOTE: You must also have the new COMCTL32.DLL (version 4.7 or later). This is installed with Internet Explorer 3 and will come standard with the new Windows 98. So if you are using VC++ 5 then you probably already have this DLL.

The API support is available in VC++ 5.0. Otherwise, you need to download the ActiveX SDK from the MS website. To use the date/time picker, do the following:

Put the following in your app's InitInstance:
        INITCOMMONCONTROLSEX icc;
        icc.dwSize = sizeof(INITCOMMONCONTROLSEX);
        icc.dwICC = ICC_DATE_CLASSES;
        InitCommonControlsEx(&icc);

Insert a custom control in your dialog where you want the date or time picker. For the class name, enter "SysDateTimePick32". For the style, see commctrl.h and search for the DTS_* style and enter the desired style (usually either DTS_SHORTDATEFORMAT or DTS_TIMEFORMAT). Be sure to make the custom control visible and have a tabstop.

You can always use CreateEx() to create them manually.

You can DDX the control to a SYSTEMTIME structure with the following code:

void AFXAPI DDX_DateTime(CDataExchange *pDX, int nIDC, 
        SYSTEMTIME &SysTime)
{
        HWND hWnd = pDX->m_pDlgWnd->GetDlgItem(nIDC)->GetSafeHwnd();
        ASSERT (hWnd != NULL);

        if (pDX->m_bSaveAndValidate)
        {
                ::SendMessage(hWnd, DTM_GETSYSTEMTIME, 0, 
                        (LPARAM)&SysTime);
        }
        else // initializing
        {
                ::SendMessage(hWnd, DTM_SETSYSTEMTIME, GDT_VALID,
                        (LPARAM)&SysTime);
        }
}

void CCtrlTestDlg::DoDataExchange(CDataExchange* pDX)
{
        CDialog::DoDataExchange(pDX);
        //{{AFX_DATA_MAP(CCtrlTestDlg)
        //}}AFX_DATA_MAP
        // m_Date and m_Time  are SYSTEMTIME structures
        DDX_DateTime(pDX, IDC_DATE, m_Date); 
        DDX_DateTime(pDX, IDC_TIME, m_Time);
}



Comments

  • Please Help me

    Posted by joselo58 on 07/05/2007 01:37pm

    I don't know how to work with DatatimePicker when I change the value, for example Today It's 5th July I want to add + 2 months. Please send me the code... Jose Luis.

    Reply
  • How to update the custom control with a date

    Posted by Legacy on 10/07/2003 12:00am

    Originally posted by: Jabberwoky

    Here's how I get and put the control data (quick and dirty) 
    
    

    CString fmtStr;
    SYSTEMTIME now;

    //Get the cureent displayed date
    GetDlgItemText(IDC_CUSTOM1, fmtStr);

    //Get today date, change date
    GetSystemTime(&now);
    now.wYear=2003;
    now.wMonth=12;
    now.wDay=01;


    HWND hWnd = GetDlgItem(IDC_CUSTOM1)->GetSafeHwnd();
    ASSERT (hWnd != NULL);

    DateTime_SetSystemtime(hWnd, GDT_VALID , &now);

    Reply
  • Read time from a edit box

    Posted by Legacy on 01/09/2003 12:00am

    Originally posted by: Stefano

    Probaly it's a stupid question but I'm not a really good programmer. I want to read the hour, according to the format hh:mm, from a edit box.
    Can you help me?

    Thank you

    Reply
  • ExCalendar

    Posted by Legacy on 01/30/2002 12:00am

    Originally posted by: Mike Philips

    If you would like to check, please feel free to download a nice calendar control here
    
    http://www.exontrol.com/sg.jsp?content=products/excalendar

    Regards, Mike

    Reply
  • Urgent!

    Posted by Legacy on 01/16/2002 12:00am

    Originally posted by: Patricia

    Can you tell me if it is possible to change the color of the text appearing in the edit? The WM_CTCOLOR seems to not work...It is quite easy to change the background color but for the text it seems impossible!
    Is it possible to display the date time control as edit only without spin?

    Reply
  • European format

    Posted by Legacy on 12/26/2001 12:00am

    Originally posted by: Malka Shafir

    Can I change the text on the bottom "Today: 12/26/01" to European format ("Today: 26/12/01")?
    If yes, I would like to know how.
    Thanks a lot.
    Malka

    Reply
  • returned dates 4 years in future

    Posted by Legacy on 08/11/2001 12:00am

    Originally posted by: sqe

    After picking a date (let's say 1.8.2001) and closing the dialog with OK, in a variable m_blahblah (a CTime object) used for ddx (i used classwizard to add that variable(s)), the value in m_blahblah is about 4 years in the future (let's say 10.3.2004)..., i use 4 callendars and they all return values the same 4 years (minus a few days) in future (if i choose 2.8.2001, it returns 11.3.2004, so it works, but a little bit strange:) ... could you help me please? (i use MS VC++ 6.0)
    thanx,

    brano

    PS: i also tryied to use CDateTimeOle (or what!, i don't know the exact name now:) object (not CTime) using classwizard, in that case, the returned dates were correct or "-1"'s, if i changed the year or month, the returned value was ok (no +4 years), but if i only clicked on some day (in actual month/year), the returned values (in .GetDay(), .GetMonth(), GetYear()) were all "-1". I am realy tired of it, please help me... ... thanx

    b.

    Reply
  • control bug?

    Posted by Legacy on 08/01/2001 12:00am

    Originally posted by: TP

    I have instantiated the control under Win32 and can select dates as normal when using the popup date grid, which works perfectly.

    However, if I attempt to modify the year value in either the "long date" or "short date" view (without opening the popup date grid), I consistently get an access violation. As far as I know, that part of the interface should be handled automatically, so is there a bug in the control?

    TP

    Reply
  • How do I change the date in run time (programmatically) ?

    Posted by Legacy on 05/24/2001 12:00am

    Originally posted by: Michael Tjong

    I use COleDateTime as the date object. When I change to a certain date in code and run it, yet still appears/display out with the current date?
    
    Any coding that I have missed? Thanks a lot for your help in advance.

    For instance:

    COleDateTime m_TodayDate;
    m_TodayDate.SetDate(1990, 05, 12);

    Michael

    Reply
  • Saving Date Ctrl to Database

    Posted by Legacy on 02/10/2001 12:00am

    Originally posted by: Kalvain

    Year and a half late post but thought I would add for anyone in the future looking for this answer.
    When you bind a Date Time Picker to a database variable the class wizard will set up a function in the data exchange that does not exist. Correction is below...

    ///Bad code inserted by class wizard
    DDX_FieldDateTimeCtrl(pDX, IDC_SR22Date, Driverptr->m_SR22DATE, Driverptr);

    ///Correction to code to make it work
    DDX_DateTimeCtrl(pDX, IDC_SR22Date, Driverptr->m_SR22DATE );

    Remove the word "Field" in the DDX function and remove the fourth parameter all together.

    Kal

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • VMware vCloud® Government Service provided by Carpathia® is an enterprise-class hybrid cloud service that delivers the tried and tested VMware capabilities widely used by government organizations today, with the added security and compliance assurance of FedRAMP authorization. The hybrid cloud is becoming more and more prevalent – in fact, nearly three-fourths of large enterprises expect to have hybrid deployments by 2015, according to a recent Gartner analyst report. Learn about the benefits of …

Most Popular Programming Stories

More for Developers

RSS Feeds