delphi中DateTimePicker设定了kind 属性为dtktime,同时format中有正确的日期格式,
如:yyyy-MM-dd HH:mm:ss,结果如下图:
之后界面上能修改日期和时间的值,例如将时间修改为:2013-08-01 12:01:01,如下图:
但读出的值日期仍然为默认的值
ShowMessage(DateTimeToStr(DateTimePicker1.DateTime));结果如下图:
以上问题解决方案如下:
在DateTimePicker1的OnChange事件中加入以下代码:
procedure TFrmMain.DateTimePicker1Change(Sender: TObject);
var
Buffer: PChar;
Size: Byte;
FSetting: TFormatSettings;
begin
FSetting.ShortDateFormat := 'yyyy-MM-dd';
FSetting.DateSeparator := '-';
FSetting.ShortTimeFormat := 'HH:mm:ss';
FSetting.TimeSeparator := ':';
Size := TDateTimePicker(Sender).GetTextLen;
inc(Size);
GetMem(Buffer, Size);
TDateTimePicker(Sender).GetTextBuf(Buffer, Size);
TDateTimePicker(Sender).DateTime := StrToDateTime(StrPas(Buffer), FSetting);
FreeMem(Buffer, Size);
end;
=================或者======================
procedure TForm1.DateTimePicker1Change(Sender: TObject);
var
Buffer: PChar;
Size: Byte;
begin
Size := TDateTimePicker(Sender).GetTextLen;
inc(Size);
GetMem(Buffer, Size);
TDateTimePicker(Sender).GetTextBuf(Buffer, Size);
//由于DateTime标准格式为yyyy/MM/dd HH:mm:ss,日期分格符为'/';
TDateTimePicker(Sender).DateTime :=
StrToDateTime(StringReplace(StrPas(Buffer),'-','/',[rfReplaceAll]));
FreeMem(Buffer, Size);
end;