【发布时间】:2011-02-28 13:45:32
【问题描述】:
当我们使用 EncodeTime 函数 EncodeTime(wHour, wMinute, wSecond, wMilliseconds) 时,它不会将毫秒值分配给 Result。
我们使用下面来编码日期和时间
Result := EncodeDate(wYear, wMonth, wDay) +
EncodeTime(wHour, wMinute, wSecond, wMilliseconds);
我们要解析为 DateTime 的字符串的值为 Apr 10 2008 7:21:31:460PM,但编码后我们得到的输出为 10/04/2008 07:21:31。
结果仅包含 HH:MM:SS 值,而不包含毫秒值。
请让我们知道是否有格式化值并将其与毫秒一起存储在变量中。 *******************我正在尝试的功能*************
function DateTimeParser(theString :string):TDateTime;
var wYear,wMonth,wDay,wHour, wMinute, wSecond,wMilliseconds : Word ;
Date,Month,Med :String;
Time : TDateTime;
testtime,testtime1 : TSystemTime;
var myDateTime : TDateTime;
begin
Month := Copy(theString,1,3) ;
if Month ='Jan' then wMonth := 01
else if Month ='Feb' then wMonth := 02
else if Month ='Mar' then wMonth := 03
else if Month ='Apr' then wMonth := 04
else if Month ='May' then wMonth := 05
else if Month ='Jun' then wMonth := 06
else if Month ='Jul' then wMonth := 07
else if Month ='Aug' then wMonth := 08
else if Month ='Sep' then wMonth := 09
else if Month ='Oct' then wMonth := 10
else if Month ='Nov' then wMonth := 11
else if Month ='Dec' then wMonth := 12
else ShowMessage('Not a Valid Month');
wYear := StrToInt(Copy(theString,8,4)) ;
wDay := StrToInt(Copy(theString,5,2)) ;
wHour := StrToInt(Copy(theString,13,2)) ;
wMinute := StrToInt(Copy(theString,16,2)) ;
wSecond := StrToInt(Copy(theString,19,2)) ;
wMilliseconds := StrToInt(Copy(theString,22,3)) ;
ShowMessage(IntToStr(wMilliseconds));
{if Copy(theString,25,2)= 'PM' then
wHour := wHour+12;}
Result := DateUtils.EncodeDateTime(wYear, wMonth, wDay,wHour, wMinute, wSecond, wMilliseconds);
//Result := Result+DateUtils.EncodeTime(wHour, wMinute, wSecond, wMilliseconds div 100);
myDateTime:= EncodeDate(2009,11,28)+EncodeTime(14,23,12,001);
ShowMessage(DatetimetoStr(myDateTime));
testtime1 := testtime;
Time :=EncodeTime(wHour, wMinute, wSecond, wMilliseconds);
ShowMessage(DateTimeToStr(Result));
**********************************************************************
end;
有什么想法吗?
【问题讨论】:
-
感谢 Adrian,我确实尝试使用该选项,但仍然没有存储毫秒值。
-
DateTimeParser的结果确实包含毫秒。但是您的测试字符串Apr 10 2008 7:21:31:460PM不起作用。您需要用 0Apr 10 2008 07:21:31:460PM填充小时。 -
在这里聚会迟到了,但您可能想要的是 AsSQLTimeStamp (docwiki.embarcadero.com/Libraries/en/…)
标签: delphi delphi-2010 dbexpress tdatetime