Using the Calendar Dialogue or DateTimePicker

For our Lazarus demonstration we added to the form a CalendarDialog (from the Dialogs tab of components), a label (from the Standard tab) and a StaticText (from the Additional tab).

Output from program CalendarDialogDemo

Output from program CalendarDialogDemo

Our code of the files needed for program CalendarDialogDemo follows. You can copy the code if necessary, but a you should be able to adapt it for your own use by inspecting the code. Again, it is the Execute function of the dialogue that is vital. The Date property is of type TDateTime, so we convert it to a string before displaying it in a label.

See the section below for a demonstration of the DateTimePicker, which you might use instead of a CalendarDialog.

uCalendarDialogDemo.pas

unit uCalendarDialogDemo;

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtDlgs,
  StdCtrls;

type
  TfrmCalendar = class(TForm)
    CalendarDialog1: TCalendarDialog;
    lblDate: TLabel;
    StaticText1: TStaticText;
    procedure FormClick(Sender: TObject);
  end; 

var
  frmCalendar: TfrmCalendar;

implementation

{$R *.lfm}

procedure TfrmCalendar.FormClick(Sender: TObject);
begin
  if CalendarDialog1.Execute then
    lblDate.Caption := 'Selected date: ' + dateToStr(CalendarDialog1.Date);
end;

end.

uCalendarDialogDemo.lfm

object frmCalendar: TfrmCalendar
  Left = 473
  Height = 76
  Top = 277
  Width = 356
  Caption = 'Calendar'
  ClientHeight = 76
  ClientWidth = 356
  OnClick = FormClick
  LCLVersion = '0.9.30'
  object lblDate: TLabel
    Left = 5
    Height = 1
    Top = 40
    Width = 1
    Font.Color = clBlue
    ParentColor = False
    ParentFont = False
  end
  object StaticText1: TStaticText
    Left = 5
    Height = 25
    Top = 8
    Width = 344
    Caption = 'Click on the form to access the calendar dialogue.'
    TabOrder = 0
  end
  object CalendarDialog1: TCalendarDialog
    Date = 40829
    OKCaption = '&OK'
    CancelCaption = 'Cancel'
    left = 192
    top = 65512
  end
end

CalendarDialogDemo.lpr

program CalendarDialogDemo;

uses
  Interfaces, Forms, uCalendarDialogDemo;

begin
  Application.Initialize;
  Application.CreateForm(TfrmCalendar, frmCalendar);
  Application.Run;
end.  

Using a DateTimePicker

The TDateTimePicker control was introduced in Lazarus 1.4. We added to a form a TDateTimePicker (Common Controls tab) and two labels, which we named lblDate and lblRelative. Double clicking in the edit box for the OnChange event of DateTimePicker1 in the Object Inspector gave the automatically generated empty procedure DateTimePicker1Change in the editor with the cursor positioned for the typing of the procedure body.

The first screenshot shows the date selection and the second shows the output.

Date selection

Date selection

Output

Output

unit uDateTimePickerDemo;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, DateTimePicker, Forms, Controls, Graphics,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    DateTimePicker1: TDateTimePicker;
    lblDate: TLabel;
    lblRelative: TLabel;
    procedure DateTimePicker1Change(Sender: TObject);
end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

procedure TForm1.DateTimePicker1Change(Sender: TObject);
var
  strDateTime: string;
  DaysBetween: integer;
begin
  DateTimeToString(strDateTime, 'dddd d mmmm, yyyy', DateTimePicker1.Date);
  lblDate.Caption := 'Selected date: ' + strDateTime;
  DaysBetween := Trunc(DateTimePicker1.Date) - Trunc(Date);
  if DaysBetween < 0 then
    lblRelative.Caption := 'Number of days since your date: ' + intToStr( -DaysBetween);
  if DaysBetween > 0 then
    lblRelative.Caption := 'Number of days until your date: '+ intToStr(DaysBetween);
  if DaysBetween = 0 then
    lblRelative.Caption := 'You have chosen today''s date.';
end;

end.
    
Programming - a skill for life!

How to use message, file, colour, font and calendar dialogue boxes to interact with the user