Home Articles Books Downloads FAQs Tips

Q: Determine the current date and time

Answer: The VCL TDateTime class is the easiest way to get the current time. The code samples below show you how. The first block of code shows how to retrieve the current time and date. The remaining code samples show some other uses for TDateTime.

//---------------------------------------------------------------------
// TASK 1: Fetching the current date and time.
// You can build a mini-clock by adding this code to an OnTimer event.
// Set the timer interval between 100 and 250 for best results.
// Note: The static CurrentDateTime method of TDateTime returns a
//       TDateTime object for the current system time and date.
//       The TimeString and DateString functions convert the object
//       to an AnsiString.
Label1->Caption = TDateTime::CurrentDateTime().TimeString();
Label2->Caption = TDateTime::CurrentDateTime().DateString();

//---------------------------------------------------------------------
// TASK 2: Setting an interval timeout value.
// First, declare a constant TDateTime object that represents an
// interval of 1000 seconds. TDateTime has quite a few different
// constructors. This one initializes the hours, minutes, seconds,
// and milliseconds values.
const TDateTime OneThousandSeconds = TDateTime(0, 16, 40, 0);

// now create a TDateTime object for the current time
TDateTime CurrentTime(TDateTime::CurrentDateTime());

// add the interval to the current time. This is too easy!
TDateTime EndTime = CurrentTime + OneThousandSeconds;

// finally, add code that checks to see if the interval has expired.
if(TDateTime::CurrentDateTime() > EndTime)
  Application->MessageBox("Times up class. Put down your pencils.",
                          "Times up" , MB_OK);

//---------------------------------------------------------------------
// TASK 3: Set an interval of 60 days from the current date.
// First, store the current date in a TDateTime object.
TDateTime CurrentDate(TDateTime::CurrentDate());

// add 60 days to the Current date
TDateTime EndDate = CurrentDate + (double)60;

// finally, add some code that checks to see if the interval has expired.
if(TDateTime::CurrentDateTime() > EndDate)
  Application->MessageBox("You evaluation period is over. This "
                          "software will self destruct in 15 seconds.",
                          "Demo period over",MB_OK);

//---------------------------------------------------------------------
// TASK 4: Create a TDateTime object from a string.
// First, we need some strings. You could provide the string with an edit
// box, but I will just declare an AnsiString to save time.
AnsiString TimeString = "10/02/97 13:15";  // 1:15 pm

// Now create the TDateTime object by passing the string in the constructor
// Note: you could also use the StrToDate, StrToTime, or StrToDateTime
//       functions in the RTL. In fact, this constructor just calls one of
//       these three RTL calls. Also note that this constructor allows
//       a second arg for limiting the string to just a date or just a time,
//       but I was unable to find the enum type for this arg in the includes
TDateTime ArmyTime(TimeString); // create an object that represents
                                // the day I got out of the Army.

// Now do something with the object
TDateTime ElapsedFreedomTime = TDateTime::CurrentDate() - ArmyTime;
unsigned short years, months, days;
ElapsedFreedomTime.DecodeDate(&years, &months, &days);
years -= 1900;  // years starts at 1900
AnsiString msg = "Elapsed time since I left the army (whoopeeee!!): " +
                 IntToStr( years)  + " years, " +
                 IntToStr( months) + " months, and " +
                 IntToStr( days) + "days.";
Application->MessageBox(msg.c_str(), "Joy",MB_OK);


Copyright © 1997-2000 by Harold Howe.
All rights reserved.