unit timeWin;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TtimeFrm = class(TForm)
    stFormats: TStaticText;
    lbFormats: TListBox;
    Timer1: TTimer;
    btnInsert: TButton;
    btnCancel: TButton;
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure lbFormatsClick(Sender: TObject);
    procedure lbFormatsDblClick(Sender: TObject);
    procedure lbFormatsKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    procedure UpdateTime;
  public
  end;

implementation

{$R *.dfm}

procedure TtimeFrm.FormCreate(Sender: TObject);
begin
  UpdateTime;
end;

procedure TtimeFrm.lbFormatsClick(Sender: TObject);
begin
  btnInsert.Enabled := lbFormats.ItemIndex <> -1;
end;

procedure TtimeFrm.lbFormatsDblClick(Sender: TObject);
begin
  if btnInsert.Enabled then
    btnInsert.Click
end;

procedure TtimeFrm.lbFormatsKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  btnInsert.Enabled := lbFormats.ItemIndex <> -1;
end;

procedure TtimeFrm.Timer1Timer(Sender: TObject);
begin
  UpdateTime;
end;

procedure TtimeFrm.UpdateTime;
const
  formats:
    array[0..15] of string =
      (
        'YYYY-MM-DD hh":"mm":"ss',
        'dddddd tt',
        'c',
        'YYYY-MM-DD',
        'DD"/"MM" "YYYY',
        'DD"/"MM"/"YY',
        'MM"/"DD"/"YY',
        'DD"."MM"."YYYY',
        'ddddd',
        'dddddd',
        'hh":"mm":"ss',
        'hh":"mm',
        't',
        'tt',
        'h":"mm":"ss am/pm',
        'h":"mm am/pm'
      );
begin
  var item := lbFormats.ItemIndex;
  lbFormats.Items.BeginUpdate;
  try
    lbFormats.Items.Clear;
    const JustNow = Now;
    for var i := 0 to High(formats) do
    begin
      const S = FormatDateTime(formats[i], JustNow);
      if lbFormats.Items.IndexOf(S) = -1 then
        lbFormats.Items.Add(S);
    end;
  finally
    lbFormats.Items.EndUpdate;
    if item <> -1 then
      lbFormats.ItemIndex := item;
  end;
end;

end.
