Delphi 2010 中的编码时间问题

发布于 2024-10-19 13:59:35 字数 2444 浏览 6 评论 0原文

当我们使用 EncodeTime 函数 EncodeTime(wHour, wMinute, wSecond, wMilliseconds) 时,它不会将毫秒值分配给结果。

我们使用下面的方法来编码日期和时间

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;

有什么想法吗?

When we use EncodeTime function EncodeTime(wHour, wMinute, wSecond, wMilliseconds) it is not assigning the millisec value to the Result.

We are using below to encode Date and Time

Result := EncodeDate(wYear, wMonth, wDay) +
  EncodeTime(wHour, wMinute, wSecond, wMilliseconds);

The String that we want to parse to a DateTime has value Apr 10 2008 7:21:31:460PM but after encoding we get the output as 10/04/2008 07:21:31.

The Result contains only HH:MM:SS value and not millisec value.

Please let us know if there is anyway to format the values and store it in a variable along with millisec.
*******************function which i am trying*************

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;

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不念旧人 2024-10-26 13:59:35

我可能误解了这里的问题,但也许它正在被存储,但你看不到它。调试器不显示毫秒,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 标记表明您正在尝试将日期时间 存储在数据库中。我不了解 dbexpress,但 ADO 会截断 datetime 中的毫秒。 要使用 ADO 在 SQL Server 中节省毫秒,您必须自己构建插入语句。 dbexpress 可能也是这样。

下面是一些 ADO 代码,它将在 SQL Server 中保存以毫秒为单位的 datetime

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

SQL Server 中 datetime 的精度为 3.33 毫秒。这与 Delphi 中的不同,因此当我保存 2011-02-28 20:43:10.012 时,它会保存为 2011-02-28 20:43:10.013在 SQL Server 中。这对你来说可能是个问题。

一种解决方案是将日期时间的毫秒部分存储在单独的整数列中。这样,您将始终存储在 Delphi 中编码的相同值,并且不必构建自己的插入语句。

DBExpress

我已经用 DBX 组件做了一些测试,它们也截断了毫秒。

I might have misunderstood the problem here but perhaps it is getting stored but you don't see it. The debugger does not show milliseconds and DateTimeToStr does not either. FormatDateTime with a format string does.

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;

Database

Your dbexpress tag suggests that you are trying to store the datetime in a database. I do not know about dbexpress but ADO truncates the milliseconds out of a datetime. To save with milliseconds in SQL Server with ADO you have to build the insert statement yourself. It might be the same with dbexpress.

Here is some ADO code that will save a datetime with milliseconds in SQL Server

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

The precision for datetime in SQL Server 3.33 milliseconds. That is not the same as in Delphi so when I save 2011-02-28 20:43:10.012 it is saved as 2011-02-28 20:43:10.013 in SQL Server. That might be a problem for you.

One solution for you could be to store the milliseconds part of a datetime in a separate integer column. That way you will always store the same value you have encoded in Delphi and you do not have to build your own insert statements.

DBExpress

I have done some testing with DBX components and they too truncates the milliseconds.

清醇 2024-10-26 13:59:35

使用此格式 HH:MM:SS.ZZZ

干杯

use this format HH:MM:SS.ZZZ

Cheers

对岸观火 2024-10-26 13:59:35

这对您来说可能并不明显,但在默认的日期和时间格式中,秒和毫秒通常用点 (.) 分隔。您在问题 Apr 10 2008 7:21:31:460PM 中显示的示例字符串在该位置有一个冒号 (:)。这很可能会导致毫秒数下降。

It may not be obvious to you, but in the default date and time formats, the seconds and milliseconds are usually separated by a dot (.). The example string you showed in your question Apr 10 2008 7:21:31:460PM has a colon (:) in that position. That could well cause the milliseconds to be dropped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文