DateTimePicker Control

Hierarchy: TObject - TPersistent - TComponent - TControl - TWinControl - TCommonCalendar - TDateTimePicker

Properties

CalColors

Calendar's colours.

Date

Enter a default date.

DateFormat

This can be ShortDate or LongDate.

DateMode

There are two modes avaialble, ComboBox style and UpDown style.

Format

There are many options for the format the date will take:

d The one- or two-digit day.
dd The two-digit day. Single-digit day values are preceded by a zero.
ddd The three-character weekday abbreviation.
dddd The full weekday name.
h The one- or two-digit hour in 12-hour format.
hh The two-digit hour in 12-hour format. Single-digit values are preceded by a zero.
H The one- or two-digit hour in 24-hour format.
HH The two-digit hour in 24-hour format. Single-digit values are preceded by a zero.
m The one- or two-digit minute.
mm The two-digit minute. Single-digit values are preceded by a zero.
M The one- or two-digit month number.
MM The two-digit month number. Single-digit values are preceded by a zero.
MMM The three-character month abbreviation.
MMMM The full month name.
t The one-letter AM/PM abbreviation (that is, AM is displayed as "A").
tt The two-letter AM/PM abbreviation (that is, AM is displayed as "AM").
yy The last two digits of the year (that is, 2001 would be displayed as "01").
yyyy The full year (that is, 2001 would be displayed as "2001").
Kind

Sets the control to a date or a time.

MaxDate

The maximum possible date the user can choose.

MinDate

The minimum possible date the user can choose.

Time

Set a default time.

Events

OnCloseUp

This event is fired when the Calendar is closed (after choosing a date).

Examples

Example 1 - Separate Date and Time

In this example we use a DateTimePicker control to choose a date. The user wants to extract and display the date and time separately.

The code:

procedure TForm1.DateTimePicker1Change(Sender: TObject);
begin
 edit1.text:=datetostr(datetimepicker1.Date);
 edit2.Text:=timetostr(datetimepicker1.Time);
end;

If we wish to enter our own time we might arrange the form thus:

And the code:

procedure TForm1.DateTimePicker1Change(Sender: TObject);
var mysdate:string;
     mydate:tdatetime;
begin
 edit1.text:=datetostr(datetimepicker1.Date);
 mysdate:=edit1.Text + edit2.text;
 mydate:=strtodatetime(mysdate);
 edit3.text:=datetimetostr(mydate);
end;

Example 2 - Matching Dates and Times

procedure TForm1.Button2Click(Sender: TObject);
begin
 if DateToStr(DateTimePicker1.Date) = '18/06/1941' then
 LabeledEdit1.Text:='Paul McArtney';
end;

Back to Tutorial

Back to Palette List