Objective Grid: Two digit years

Article ID: 78
Last updated: 22 Jun, 2018
Article ID: 78
Last updated: 22 Jun, 2018
Revision: 3
Views: 2251
Posted: 11 Jan, 2001
by Meltreger B.
Updated: 22 Jun, 2018
by Meltreger B.

Problem


When I'm using a two-digit year, how can I force years 00 to 49 to be 2000 through 2049?


Cause



Action


You can do year checking forcing 50-99 to be 1950-1999 and 00-49 to be 2000-2049 by using the code below to subclass CGXDateTimeCtrl and override the OnChanging() method. This has been fixed for the 7.01 release.

//declaration for my datetime control class
class CGXMyDateTimeCtrl: public CGXDateTimeCtrl
{ 
      DECLARE_CONTROL(CGXMyDateTimeCtrl)
 public:
      CGXMyDateTimeCtrl(CGXGridCore* pGrid, UINT nID);
      virtual BOOL OnChanging(const COleDateTime& newDateTime);
};

//implementation  for my datetime control class
IMPLEMENT_CONTROL(CGXMyDateTimeCtrl, CGXDateTimeCtrl);

CGXMyDateTimeCtrl::CGXMyDateTimeCtrl(CGXGridCore* pGrid, UINT nID):CGXDateTimeCtrl(pGrid, nID)
{ }

BOOL CGXMyDateTimeCtrl::OnChanging(const COleDateTime& newDateTime)
{
    int nOldYear = m_datetime.GetYear() % 100;
    int nYear = newDateTime.GetYear() % 100;
    if( nOldYear != nYear )
     { 
        if( nYear >50 )
             nYear += 1900;
        else
             nYear += 2000;
        m_datetime.SetDate( nYear, newDateTime.GetMonth(), newDateTime.GetDay());  //put the correct year in the time member
        UpdateGadgets();                     // update the gadgets with the proper value
        SetModify(TRUE);
        return FALSE;                          
     }
     return CGXDateTimeCtrl::OnChanging(newDateTime);
}

//  To use CGXMyDateTimeCtrl, you would include code such as this to register the control in your OnInitialUpdate 
//  where IDS_CTRL_DATETIME is a string resource and is used in SetControl to specify this control.

         CGXMyDateTimeCtrl* pDateTime = new CGXMyDateTimeCtrl(this,  IDS_CTRL_DATETIME);
         pDateTime->CreateControl(WS_CHILD|GXDT_DTS_UPDOWN|GXDT_DTS_CALENDAR, CRect(0,0,0,0));
         // If you don't want spin buttons remove the GXDT_DTS_UPDOWN flag above!
         pDateTime->m_bInactiveDrawButton = FALSE;
         pDateTime->m_bInactiveDrawSpin = FALSE;
         pDateTime->m_bDateValueAsNumber = FALSE; // set this TRUE if values should be stored
                                                      // as number and not as date string. This is especially very essential if you
                                                      // need support for differnt languages, e.g. if a document gets serialized with
                                                     // an english computer and reloaded on a german computer.

          RegisterControl ( IDS_CTRL_DATETIME,  pDateTime);

This article was:   Helpful | Not helpful
Report an issue
Article ID: 78
Last updated: 22 Jun, 2018
Revision: 3
Views: 2251
Posted: 11 Jan, 2001 by Meltreger B.
Updated: 22 Jun, 2018 by Meltreger B.

Others in this category