您的位置:首页 > 编程语言 > Delphi

Delphi关于时间的函数

2008-01-28 13:38 295 查看
use DateUtils;(此单元里有许多关于时间的函数)。我今天就只看了这么多了,还有许多读者自己去看吧。
里面有太多的关于时间的函数了,如:输入一个时间,可以提取date或time部分,此月有多少天,比较两个时间的’大小’,…..自己看帮助。
function CompareDate(const A, B: TDateTime): TValueRelationship;比较A,B日期哪个时间先发生?还是同时发生?
function CompareDateTime(const A, B: TDateTime): TValueRelationship
function DaysBetween(const ANow, AThen: TDateTime): Integer;两时间相隔多少天?
function DaysInAMonth(const AYear, AMonth: Word): Word;某年某月有多少天
function DaysInAYear(const AYear: Word): Word;某年有多少天。
function DaysInMonth(const AValue: TDateTime): Word;某月有多少天
procedure DecodeDateMonthWeek(const AValue: TDateTime; out AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word);对应out参数:哪年,哪月,此月的第几周,是周几。
function EncodeDateDay(const AYear, ADayOfYear: Word): TDateTime;输入某年,此年第几天,out得到此天日期。

DayOfWeek(TDateTime)某个时间是星期几?
DayOfTheYear(TDateTime)一年的第几天。
function DayOfTheMonth(const AValue: TDateTime): Word;第几天of the Month.
function DateOf(const AValue: TDateTime): TDateTime;提取时间部分
这个方法可以完成你的要求。用法可见帮助示例。
procedure DecodeDateTime(const AValue: TDateTime; out AYear, AMonth, ADay,
AHour, AMinute, ASecond, AMilliSecond: Word); (注它必须引用DateUtils单元);
它把时间的各个部分都分离出来了。与之对应的函数为:
function EncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word):TDateTime;
function IncMinute (const AValue: TDateTime; const ANumberOfMinute: Int64 = 1): TDateTime;
例:IncMinute(now,N);增加N分钟在NOW的基础上。可以为负。
---------------------------------------------------------------------------------------------------------------------------------------------

用于时间操作的函数

Date函数:

定义:Date: TDateTime;

作用:返回当前的日期

范例:CurrentDate := Date;
DayOfWeek函数:


定义:function DayOfWeek(Date: TDateTime): Integer;

作用:得到指定日期的星期值,返回1~7,代表周日到周六。
IncMouth函数:
定义:function IncMonth(const Date: TDateTime; NumberOfMonths: Integer): TDateTime;


作用:求给定的日期Data在NumberOfMonth 个月后的日期。

范例:Date1 := IncMonth(date,10);

如果今天是2002-5-3,则Date1就是2003-3-3

IsLeapYear函数:
定义:function IsLeapYear(Year: Word): Boolean;


作用:判断指定的年份是否是闰年;

范例:if isLeapYear(2000) then ShowMessage(‘该年是闰年’);

Now函数:

定义:function Now: TDateTime;
作用:用于取得当前的日期时间


范例:CurrentDateTime := Now;

ReplaceDate 过程

定义:procedure ReplaceDate(var DateTime: TDateTime; const NewDate: TDateTime);

作用:使用参数Newdate日期部分替换参数DateTime的日期部分,但不改变时间部分
ReplaceDate 过程


定义:procedure ReplaceTime(var DateTime: TDateTime; const NewTime: TDateTime);

作用:使用参数Newdate时间部分替换参数DateTime的时间部分,但不改变日期部分。

如果上述这两个过程一起用,就相当于赋值了。
Time函数:


定义:function Time: TDateTime;

作用:返回当前的时间

范例:CurrentTime := Time;
DateTimeToFileDate函数:


定义:DateTimeToFileDate(DateTime: TDateTime): Integer;

作用:将一个TDateTime类型的时间转化为Dos环境中的时间,Dos环境下对时间的访问方法和VCL中的TdateTime类型不一样,在进行文件操作时,为了保持时间的一致性,需要使用DateTimeToFileDate函数进行转化,返回的Integer的值就是Dos下的用于描述时间的值。

DateTimeToSystemTime 过程:

定义:procedure DateTimeToSystemTime(DateTime: TDateTime; var SystemTime: TSystemTime);

作用:将一个TDateTime类型的时间转换为Win API函数所使用的TSystemTime类型,在使用WinApi函数操纵时间时用到。

SystemTimeToDateTime 函数:

定义:function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime;

作用:将一个在WinApi函数中得到的TSysTemTime类型的数转换为TDateTime类型。

DateTimeToTimeStamp 函数:

TimeStampToDateTime 函数:

定义:DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;

function TimeStampToDateTime(const TimeStamp: TTimeStamp): TDateTime;

作用:用于在TDataTime类型与TTimeStamp之间进行互相转换。TDataTime 用一个double描绘一个时间,而TTimeStamp则是采用两个整形数分别描述时间。两种类型的区别可参考文章开始处的数据类型描述部分。

EncodeDate 函数:

定义:function EncodeDate(Year, Month, Day: Word): TDateTime;

作用: 输入年(year),月(month),日(day)的值,将该日期返回为TDateTime类型,年的范围为1-9999,月份的范围为1-12,日期的范围视当月的情况而定,如果输入的值超出范围,则将产生一个EConvertError错误。
DecodeDate 过程:


定义:procedure DecodeDate(Date: TDateTime; var Year, Month, Day: Word);

作用:输入一个TDateTime类型的日期,将其转为年(Year),月(Month),日(Day)的值。如果输入值为0或小于0,则年月日均为0,

EncodeTime 函数:

定义:EncodeTime(Hour, Min, Sec, MSec: Word): TDateTime;

作用:输入小时(Hour),分(min),秒(Sec),微秒(MSec)的值,返回一个TDateTime类型的时间,该值为一个介于0至1之间的小数。Hour的取值范围为0-23,Min的取值范围为0-59,Sec的取值范围为0-59,MSec的取值范围为0-999,如果输入值超出范围,则产生一个EConvertError 错误。

DecodeTime 过程:

定义:procedure DecodeTime(Time: TDateTime; var Hour, Min, Sec, MSec: Word);

作用:输入一个时间,将其转换为小时(Hour),分(min),秒(Sec),微秒(MSec)的值。
TDateTime类型与字符串型转换函数:


DateTimeToStr 函数:

DateToStr 函数

TimeToStr函数

定义:function DateTimeToStr(DateTime: TDateTime): string;

function TimeToStr(Time: TDateTime): string;

function TimeToStr(Time: TDateTime): string;

作用:将一个TDateTime类型的数转换成字符串,DateTimeToStr转换日期和时间,DateToStr只转换日期,TimeToStr只转换时间。转换后的输出效果为YYYY-M-D H:M:S

StrToDateTime函数

StrToDate函数

StrToTime函数

定义:function StrToDateTime(const S: string): TDateTime;

function StrToDate(const S: string): TDateTime;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: