博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

TDateTime

Posted on 2011-07-07 09:46  豪杰的爸爸  阅读(782)  评论(0编辑  收藏  举报
8.6 日期时间类
8.6 日期时间类
此类函数和过程主要定义在SysUtils 和DateUtils 两个单元。
8.6.1 获取/合成日期/时间
function Now: TDateTime;
返回当前日期和时间。
function Date: TDateTime;
返回当前日期。
function Time: TDateTime;
返回当前时间。
function YearOf(const AValue: TDateTime): Word;
返回指定日期
/时间的年份。类似的有:
MonthOf、WeekOf、DayOf、HourOf、MinuteOf、SecondOf、MilliSecondOf。
如果要一次取得其中的多项,可以使用:
procedure DecodeDate(Date: TDateTime; var Year, Month, Day: Word);
procedure DecodeTime(Time: TDateTime; var Hour, Min, Sec, MSec: Word);
procedure DecodeDateTime(DateTime: TDateTime; var Year, Month, Day, Hour,
Minute, Second, MilliSecond: Word);
如果要合成一个日期
/时间,可以使用:
function EncodeDate(Year, Month, Day: Word): TDateTime;
function EncodeTime(Hour, Min, Sec, MSec: Word): TDateTime;
function EncodeDateTime(Year, Month, Day, Hour, Minute, Second,
MilliSecond: Word): TDateTime;
procedure ReplaceDate(var DateTime: TDateTime; const NewDate: TDateTime);
更新DateTime 的日期部分。
procedure ReplaceTime(
var DateTime: TDateTime; const NewTime: TDateTime);
更新DateTime 的时间部分。类似的有:
RecodeYear
/Month/Day 等等可以更新年份、月份、天等数据。
8.6.2 日期/时间和字符串的转换
function DateToStr(Date: TDateTime): string;
function TimeToStr(Time: TDateTime): string;
function DateTimeToStr(DateTime: TDateTime): string;
function StrToDate(const S: string): TDateTime;
function StrToTime(const S: string): TDateTime;
function StrToDateTime(const S: string): TDateTime;
8.6.3 日期/时间的运算
日期
/时间数据在本质上是一个浮点数,在System 单元可以看到:
TDateTime 
= type Double;
也就是说,TDateTime 不过是Double 的别名。该数据的整数1 表示一天,如:
var
dt: TDateTime;
begin
dt :
= Now; {现在}
dt :
= dt + 1{明天}
dt :
= dt + 0.25{6 小时后,因为6 小时为0.25 天}
end;
VCL 提供以下函数和过程封装了这些原始运算:
function IncYear(const AValue: TDateTime;
const ANumberOfYears: Integer = 1): TDateTime;
对年份运算。类似的有IncMonth、IncDay 等。ANumberOfYears 可以是正数或负数。
function WeeksInYear(const AValue: TDateTime): Word;
一年中有多少周。
  
function DaysInYear(const AValue: TDateTime): Word;
一年中有多少天。
function IsInLeapYear(const AValue: TDateTime): Boolean;或者
function IsLeapYear(Year: Word): Boolean;
是否是闰年。
function YearSpan(const ANow, AThen: TDateTime): Double;
两个日期相差多少年。它假设一年为365.
25 天。
function YearsBetween(const ANow, AThen: TDateTime): Integer;
它是Trunc(YearSpan)(直接取整数,不四舍五入)的结果。
类似的有MonthSpan
/sBetween、DaySpan/sBetween、WeekSpan/sBetween、HourSpan/sBetween 等。
function SystemTimeToDateTime(const SystemTime: TSystemTime):
TDateTime;
将一个系统类型时间转化为TDateTime 类型。
可用API 函数GetSystemTime(SystemTime: PSystemTime)取得系统时间。