Dates and Times

The data type for a date is TDateTime. This is derived from the SysUtils unit.

The TDateTime stores a date and time in a Double as the number of days since the start of the day on December 30, 1899. The integer part is the number of days, the fractional part is the number of days. 

There are numerous functions for handling dates and times in the SysUtils unit:

Date - returns current date in local time zone.

Time - returns current time in local time zone

DateTimeToStr(DateTime: TDateTime): string; - formats a DateTime as a string, formatting the date in ShortDateFormat, followed by a space and the time formatted in LongTime Format. 

DateToStr(Date: TDateTime): string; - formats date part of a DateTime type to a string in ShortDateFormat.

DateToTimeStamp - converts DateTime to TimeStamp format.

DayOfWeek - returns day of week, Sunday=1, Saturday=7.

DecodeDate - procedure - extracts year, month and day from a DateTime type. Months in range 1-12.

DecodeTime - procedure - extracts hours, minutes, seconds and milliseconds from a DateTime type.

IsLeapYear(Year: Word): Boolean; - returns true if supplied year is a leap year.

Now: TDateTime - returns current date and time in local time zone.

StrToDate(const S: string): TDateTime; - parses a string to obtain a date. String must convert a valid date in ShortDateFormat format. If string is invalid an EConvertError exception is raised.

StrToDateTime(const S: string): TDateTime; - parses a string to obtain a date and a time. String must contain a valid date in ShortDateFormat format and a time in Str. If string is invalid an EConvertError exception is raised.

StrToTime(const S: string): TDateTime; - parses a string to obtain a time. If string is invalid an EConvertError exception is raised.

SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime; - converts system time to DateTime.

TimeToStr(Time: TDateTime): string; - formats time part of a DateTime as a string;

Variables

DateSeparator - character used to separate elements of date. default taken from Windows locale.

TimeStamp - stores a date and time in separate parts:

type
  TTimeStamp = record
    Time: integer;          //milliseconds since midnight
    Date: integer;          //days since January 1st 
  end;

TimeSeparator - character used to separate elements of time.

Examples:

 

procedure TForm1.Edit1Exit(Sender: TObject);
begin
  mydate:=StrToDate(edit1.Text);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit2.Text:=DateToStr(mydate);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Edit3.Text:=DateTimeToStr(now);
end;

Changing the Date Separator

You may want to incorporate the date into the name of a file, for example as part of a backup routine where a copy of a file is taken each day and the date is used as part of the name. In this situation the '/' character in a date would prevent the file being stored correctly so you would want to change the separator character. Thus we might have:

dateseparator:='_'; //underscore character
copyfileto ('c:\access\companydata.mdb', 'c:\backup\companydata' + strtodate(now) + '.mdb');
//copyfileto needs IDGlobal unit in the units declaration section

This appends a date in the format '12_12_2004' to the file name 'companydata'.

Back to Delphi Menu