whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

90 Visitors Online


 
...use a custom format in TDateTimePicker?
Autor: Thomas Stutz
[ Print tip ]  

Tip Rating (20):  
     




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;


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners