DelphiBasics
TDateTime
Type
Data type holding a date and time value System unit
type TDateTime = type Double;
Description
The TDateTime type holds a date and time value.
 
It is stored as a Double variable, with the date as the integral part, and time as fractional part. The date is stored as the number of days since 30 Dec 1899. It really should be 31 Dec is.
 
It appears that the reason for Delphi starting at 30 Dec 1899 is to make it as compatible as possible with Excel while at the same time not adopting Excel's incorrectness about dates. Historically, Excel played second fiddle to Lotus 1-2-3. Lotus (which may have got this error from Visicalc) incorrectly considered 1900 to be a leap year hence a value of 60 gives you 29 Feb 1900 in Excel but is interpreted as 28 Feb 1900 in Delphi due to Delphi starting 1 day before. From the 01 Mar 1901 the two date systems give the same result for a given number.
 
01 Jan 1900 has a days value of 2.
 
Because TDateTime is actually a double, you can perform calculations on it as if it were a number. This is useful for calculations such as the difference between two dates.
Notes
No local time information is held with TDateTime - just the day and time values.
Related commands
DateTimeToFileDateConvert a TDateTime value to a File date/time format
DateTimeToStrConverts TDateTime date and time values to a string
DateTimeToStringRich formatting of a TDateTime variable into a string
PDateTimePointer to a TDateTime value
StrToDateTimeConverts a date+time string into a TDateTime value
 Download this web site as a Windows program.




 
Example code : Finding the difference between two dates
var
  day1, day2 : TDateTime;
  diff : Double;
begin
  day1 := StrToDate('12/06/2002');
  day2 := StrToDate('12/07/2002');
  ShowMessage('day1 = '+DateToStr(day1));
  ShowMessage('day2 = '+DateToStr(day2));

  diff := day2 - day1;
  ShowMessage('day2 - day1 = '+FloatToStr(diff)+' days');
end;
Show full unit code
  day1 = 12/06/2002
  day2 = 12/07/2002
  day2 - day1 = 30 days
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page