2011-02-28 119 views
2

當我們使用EncodeTime函數EncodeTime(wHour,wMinute,wSecond,wMilliseconds)時,它不會將millisec值分配給結果。在Delphi 2010中編碼時間問題

我們使用以下編碼日期和時間

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值,而不包含millisec值。

請讓我們知道,無論如何格式化值並將其與millisec一起存儲在變量中。 * ** * ** * ** * ** * ** * ***功能,我試圖* ** * ** * ** * ***

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; 

任何想法?

+1

看到這個答案:http://stackoverflow.com/questions/1760929/how-to-encode-a-datetime-in-delphi/1760943#1760943 – Adrian 2011-02-28 14:42:53

+0

謝謝阿德里安,我沒有嘗試使用該選項,但仍millisec價值沒有得到存儲。 – SSE 2011-02-28 14:51:18

+0

「DateTimeParser」的結果確實包含毫秒。但是你的測試字符串'Apr 10 2008 7:21:31:460PM'不起作用。您需要用0'Apr 10 2008 07:21:31:460PM'填充小時。 – 2011-03-01 10:23:04

回答

2

使用這種格式HH:MM:SS.ZZZ

乾杯

2

它可能不是很明顯給你,但在默認的日期和時間格式,秒和毫秒點通常是分離(.)。您在問題Apr 10 2008 7:21:31:460PM中顯示的示例字符串在該位置有一個冒號(:)。這很可能會導致毫秒的丟失。

+0

嗨格式化後,發現db express正在修剪millisec部分發送到數據庫。我從數據庫中獲取下面的錯誤消息。例如:'日期不符合通過日期:01/03/2011 10:42:05.460現有日期shd爲:01/03/2011 10:42:05.463'有沒有什麼辦法可以用db express來解決這個問題 – SSE 2011-03-01 11:23:25

+0

@ user637761:不知道dbexpress足夠好地說出這種或那種方式,但它可能是正在進行修整的數據庫。我知道,例如SQL服務器確實會丟失傳遞給它的毫秒數。你可以試試TIMESTAMP列而不是DATETIME(不知道確切的名字)。也許TIMESTAMP列在您使用的數據庫中確實有毫秒級的分辨率。 – 2011-03-01 12:23:31

6

我可能誤解了這裏的問題,但也許它正在存儲,但你沒有看到它。調試器不顯示毫秒,DateTimeToStr也不顯示。 FormatDateTime使用格式字符串。

var 
    Date: TDateTime; 
begin 
    Date := EncodeDateTime(2011, 02, 28, 20, 43, 10, 12); 

    //DateTimeToStr does not show milliseconds 
    ShowMessage(DateTimeToStr(Date)); 

    //Use FormatDateTime with Format string 
    ShowMessage(FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz' , Date)); 
end; 

數據庫

dbexpress標籤表明您正試圖將datetime存儲在數據庫中。我不知道dbexpress,但ADO截斷了datetime的毫秒數。 To save with milliseconds in SQL Server with ADO您必須自己構建插入語句。這可能與dbexpress相同。

下面是一些ADO代碼將與毫秒節省datetime在SQL Server

ADOCommand1.CommandText := 'insert into DateTbl values ('''+ 
    FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz' , Date)+''')'; 
ADOCommand1.Execute; 

在SQL Server 3.33毫秒爲datetime精度。這與Delphi不一樣,因此當我保存2011-02-28 20:43:10.012時,它在SQL Server中保存爲2011-02-28 20:43:10.013。這對你來說可能是個問題。

對於您來說,一種解決方案可能是將datetime的毫秒部分存儲在單獨的整數列中。這樣,您將始終存儲您在Delphi中編碼的相同值,並且您不必構建自己的插入語句。

的dbExpress

我已經做了一些測試與DBX成分,他們也截斷毫秒。

+0

謝謝..我正在使用Sybase ..我嘗試格式化.. – SSE 2011-03-01 09:36:27

+0

嗨格式化後,發現db express正在修剪millisec部分,同時發送到數據庫。我從數據庫中獲取下面的錯誤消息。例如:'日期不匹配通過日期:01/03/2011 10:42:05.460現有日期shd爲:01/03/2011 10:42:05.463' – SSE 2011-03-01 11:18:25

+0

@SSE - 使用AsSQLTimeStamp代替。這保留了毫秒。 – 2012-10-05 00:29:13