...use a custom format in TDateTimePicker?

Author: Thomas Stutz

Category: VCL

uses
  
ComCtrls;

const
  
DTM_SETFORMAT = $1005;

procedure TForm1.Button1Click(Sender: TObject);
var
  
sFormat: string;
begin
  
DateTimePicker1.kind := dtkTime;

  // Don't show the seconds, Sekunden nicht anzeigen
  
SendMessage(DateTimePicker1.Handle, DTM_SETFORMAT, 0, Longint(PChar('hh:mm')));

  // To show AM/PM
  
SendMessage(DateTimePicker1.Handle, DTM_SETFORMAT, 0, Longint(PChar('hh:mm tt')));

  // 24-hour clock: Be sure to set Kind to dtkTime
  
DateTime_SetFormat(DateTimePickerTest.Handle, pChar('H:mm:ss'));
  
  // To show Date and Time, Datum und Zeit anzeigen
  // Note that you can only edit the date or the time depending
  // on the Kind (dtkTime or dtkDate).
  
SendMessage(DateTimePicker1.Handle, DTM_SETFORMAT, 0, Longint(PChar('dd/MM/yyyy hh:ss')));

  // You could also use the DateTime_SetFormat macro:
  
DateTime_SetFormat(DateTimePicker1.Handle, PChar('M/d/yyy'));
  
  // Instead of using the DateTime_SetFormat function, you can
  // send a message to the control directly:
  
sFormat := 'dd-MMMM-yyyy';
  DateTimePicker1.Perform(DTM_SETFORMAT, 0, DWORD(sFormat));
end;

// Show the Week Number in a TDateTimePicker

procedure DateToWeek(dtDate: TDateTime; var AWeek, AYear: Word);
const
  
FIRST_WEEKDAY: Integer = 2;
  FIRST_WEEKDATE: Integer = 4;
var
  
wMonth, wDay: Word;
begin
  
dtDate := dtDate - ((DayOfWeek(dtDate) - FIRST_WEEKDAY + 7) mod 7) + 7 - FIRST_WEEKDATE;
  DecodeDate(dtDate, AYear, wMonth, wDay);
  AWeek := (Trunc(dtDate - EncodeDate(AYear, 1, 1)) div 7) + 1;
end;

procedure TForm1.DateTimePicker1Change(Sender: TObject);
var
  
sFormat: string;
  wWeek, wYear: Word;
begin
  
DateToWeek(DateTimePicker1.date, wWeek, wYear);
  sFormat := 'dd/MM/yy Week:' + IntToStr(wWeek) + '';
  DateTimePicker1.Perform(DTM_SETFORMAT, 0, DWORD(sFormat));
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  
DateTimePicker1Change(Self);
end;

 

printed from
www.swissdelphicenter.ch
developers knowledge base