如何在Delphi中编码datetime [英] How to encode a datetime in delphi

查看:169
本文介绍了如何在Delphi中编码datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在Delphi中使用编码将个人YY,MM和DD编码成datetime字段,或者使用encodetime将单个HH,SS,MM和MS编码为datetime字段,但是有没有一种方法来指定日期和时间转换成datetime字段?



Coz与编码我无法指定时间和编码时间我无法指定日期...


$ b $例如如何设置日期时间字段为2009-11-28 14:23:12.000



请帮助。



谢谢。

解决方案

尝试使用 EncodeDateTime 函数在DateUtils单元中声明。

  function EncodeDateTime(const AYear:Word; 
const AMonth:Word;
const ADay:Word;
const AHour:Word;
const AMinute:Word;
const ASecond:Word;
const AMilliSecond:Word):TDateTime;

看到这个例子

 使用
DateUtils;

var
myDateTime:TDateTime;

begin

//您的代码
myDateTime:= EncodeDateTime(2009,11,28,14,23,12,000);
//您的代码


结束;

另一个选项

 使用
SysUtils;

var
myDateTime:TDateTime;
begin
//您的代码
myDateTime:= EncodeDate(2009,11,28)+ EncodeTime(14,23,12,000);
//你的代码
end;

第二个选项的工作原理是因为 TDatetime 存储为Double( TDateTime = type Double; ),其日期为组成部分( EncodeDate 函数返回积分)和时间作为小数部分。



TDateTime的日期部分表示自12/30/1899以来已经过去的天数。 TDateTime可以是任何日期,直到9999年12月31日(十进制值2,958,465),TDateTime值也可以是负数。小数值-693593对应于0001年1月1日。



请参阅这些示例

  var 
myDateTime:TDateTime;

开始
myDateTime:= 0; //表示12/30/1899
myDateTime:= 1; //代表12/31/1899
myDateTime:= - 1; //代表12/29/1899
myDateTime:= - 693593; //代表01/01/0001
myDateTime:= Now(); //将当前日期和时间分配给myDateTime

myDateTime:= Trunc(Now()); //仅提取日期部分。

myDateTime:= Frac(Now()); //提取时间部分。

myDateTime:= Now()+ 1; //向当前日期时间添加一天


结束;

embarcadero网站的重要提示:


要查找两个日期之间的分数天数
,只需减去
两个值,除非
System.TDateTime中的一个值为负数。
同样地,为了将一个日期和
的时间值增加一定的小数
天数,可以将小数
数字加到日期和时间值如果
系统.TDateTime值为
为正值。



当使用负
System.TDateTime值时,计算
必须处理时间部分单独

小数部分反映$ 24

分数,而
符合
System.TDateTime值的符号。例如,
在12/29/1899上午6:00是-1.25,而不是-1
+ 0.25,这将是-0.75。在
-1和0之间没有System.TDateTime值。


对于附加信息,您可以看到此链接




I know how to use encodedate in Delphi to encode individual YY, MM and DD into a datetime field or use encodetime to encode individual HH, SS, MM and MS into datetime field but is there a way to specify both date and time into a datetime field?

Coz with encodedate I cannot specify the time and with encodetime I cannot specify the date...

e.g. how can I set a datetime field to 2009-11-28 14:23:12.000

Please help.

Thanks.

解决方案

Try using the EncodeDateTime function declarated in the DateUtils unit.

function EncodeDateTime(const AYear: Word;
    const AMonth: Word;
    const ADay: Word;
    const AHour: Word;
    const AMinute: Word;
    const ASecond: Word;
    const AMilliSecond: Word): TDateTime;

See this example

uses
DateUtils;

var
  myDateTime : TDateTime;

begin

 //Your Code
 myDateTime := EncodeDateTime(2009, 11, 28, 14, 23, 12, 000);
 //Your Code


End;

Another option

uses
SysUtils;

var
myDateTime : TDateTime;
begin
 //Your Code
 myDateTime:= EncodeDate(2009,11,28)+EncodeTime(14,23,12,000);
 //Your Code    
end;

The second option works because the TDatetime It is stored as a Double (TDateTime = type Double;), with the date as the integral part (the EncodeDate function returns the integral), and time as fractional part.

The date part of the TDateTime represents the number of days that have passed since 12/30/1899. a TDateTime can be any date through 31 Dec 9999 (decimal value 2,958,465), TDateTime values can also be negative. The decimal value -693593 corresponds to 1 Jan 0001.

see theses examples

var
myDateTime : TDateTime;

Begin
myDateTime :=0; //represents 12/30/1899
myDateTime :=1; //represents 12/31/1899
myDateTime :=-1; //represents 12/29/1899
myDateTime :=-693593; //represents 01/01/0001
myDateTime := Now(); //assign the current date and time to myDateTime 

myDateTime:=Trunc(Now()); //Extract only the date part.

myDateTime:=Frac(Now()); //Extract only the time part.

myDateTime :=Now() + 1;// Add a day to the current datetime


End;

Important Note from embarcadero site :

To find the fractional number of days between two dates, simply subtract the two values, unless one of the System.TDateTime values is negative. Similarly, to increment a date and time value by a certain fractional number of days, add the fractional number to the date and time value if the System.TDateTime value is positive.

When working with negative System.TDateTime values, computations must handle time portion separately. The fractional part reflects the fraction of a 24-hour day without regard to the sign of the System.TDateTime value. For example, 6:00 am on 12/29/1899 is –1.25, not –1 + 0.25, which would be –0.75. There are no System.TDateTime values between –1 and 0.

for addtional information you can see this link

这篇关于如何在Delphi中编码datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆