Delphi 常用日期与时间函数.docx
《Delphi 常用日期与时间函数.docx》由会员分享,可在线阅读,更多相关《Delphi 常用日期与时间函数.docx(64页珍藏版)》请在冰豆网上搜索。
![Delphi 常用日期与时间函数.docx](https://file1.bdocx.com/fileroot1/2022-11/22/8dcb87ea-6019-4c9d-b082-c2136dbee4b3/8dcb87ea-6019-4c9d-b082-c2136dbee4b31.gif)
Delphi常用日期与时间函数
Delphi常用日期与时间函数
1获取特定的日期与时间
本小节将为您介绍Delphi所提供的获取特定日期与时间的函数.这些函数稍后将有详细的范例说明.笔者这里将以列表的方式先说明每一个函数所代表的意义,如图示:
函数名称
单元文件
所代表的意义
Now
SysUtils
此函数可返回现在的日期与时间,其返回值为TDateTime类型
Date
SysUtils
此函数可返回现在的日期,其返回值为TDateTime类型
Time
SysUtils
此函数可返回现在的时间,其返回值为TDateTime类型
Today
DateUtils
此函数可返回今天的日期,其返回值为TDateTime类型,此函数的结果与Date函数相同.
Tomorrow
DateUtils
此函数可返回昨天的日期,其返回值为TDateTime类型
Yesterday
DateUtils
此函数可返回明天的日期,其返回值为TDateTime类型
Currentyear
SysUtils
此函数可返回现在所属的年度,其返回值为4的整数.例如:
2001
Hoursperday
SysUtils
此常数定义每天的小时数.HoursPerDay=24;
Minsperday
SysUtils
此常数定义每天的分钟数.MinsPerDay=MinsPerDay*60
Secsperday
SysUtils
此常数定义每天的秒数.SecPerDay=MinsPerDay*60
msecsperday
SysUtils
此常数定义每天的毫秒数.MSecsPerDay=SecsperDay*1000
Now(返回当前的日期时间)
引用单元:
SysUtils
函数声明:
FunctionNow:
TDateTime;
范例D-1
ProcedureTForm1.Button1Click(Sender:
TObject);
Var
MyDateTime:
TDateTime;
begin
MyDateTime:
=Now;
Showmessage(DateTimeToStr(MyDateTime));
end;
Date(返回当前的日期)
引用单元:
SysUtils
函数声明:
FunctionDay:
TDateTime;
范例D-2
ProcedureTForm1.Button1Click(Sender:
TObject);
Var
MyDateTime:
TDateTime;
begin
MyDateTime:
=Date;
Showmessage(DateTimeToStr(MyDateTime));
end;
Time(返回当前的时间)
引用单元:
SysUtils
函数声明:
FunctionTime:
TDateTime;
范例D-3
ProcedureTForm1.Button1Click(Sender:
TObject);
Var
MyDateTime:
TDateTime;
begin
MyDateTime:
=Time;
Showmessage(DateTimeToStr(MyDateTime));
end;
Today(返回今天的日期)
引用单元:
DateUtils
函数声明:
FunctionToday:
TDateTime;
范例D-4
ProcedureTForm1.Button1Click(Sender:
TObject);
Var
MyDateTime:
TDateTime;
Begin
//usesDateUtils
MyDateTime:
=Today;
Showmessage(DateTimeToStr(MyDateTime));
end;
Tomorrow(返回明天的日期)
引用单元:
DateUtils
函数声明:
FunctionTomorrow:
TDateTime;
范例D-5
ProcedureTForm1.Button1Click(Sender:
TObject);
Var
MyDateTime:
TDateTime;
Begin
//usesDateUtils
MyDateTime:
=Tomorrow;
//MyDateTime:
Now+1;//两者相同
Showmessage(DateTimeToStr(MyDateTime));//不包含时间部分
end;
Yesterday(返回昨天的日期)
引用单元:
DateUtils
函数声明:
FunctionYesterday:
TDateTime;
范例D-6
ProcedureTForm1.Button1Click(Sender:
TObject);
Var
MyDateTime:
TDateTime;
Begin
//usesDateUtils
MyDateTime:
=Yesterday;
//MyDateTime:
Now-1;//两者相同
Showmessage(DateTimeToStr(MyDateTime));//不包含时间部分
end;
CurrentYear(返回现在所属的年度)
引用单元:
SysUtils
函数声明:
FunctionCurrentYear:
Word;
范例D-7
ProcedureTForm1.Button1Click(Sender:
TObject);
Var
ThisYear:
Word;
Begin
thisyear:
=CurrentYear;
Showmessage(IntToStr(ThisYear));//4位整数
end;
HoursPerDay,MinsPerDay,SecsPerDay及MsecsPerDay等日期与时间常数.
引用单元:
SysUtils
函数声明:
FunctionCurrentYear:
Word;
范例D-8
ProcedureTForm1.Button1Click(Sender:
TObject);
Begin
Showmessage('每天的小时数='+IntToStr(HoursperDay));//24
Showmessage('每天的分钟数='+IntToStr(MinsperDay));//1440
Showmessage('每天的秒数='+IntToStr(SecsperDay));//24
Showmessage('每天的毫秒数='+IntToStr(MSecsperDay));//24
end;
D.2日期处理函数:
函数名称
单元文件
所代表的意义
YearOf
DateUtils
此函数可获取TDateTime格式中的年度,其返回值为Word类型.
YearsBeTween
DateUtils
此函数可返回两个指定日期间的年份,一年以365.25天为计算单位.其返回值不包含小数部分
YearSpan
DateUtils
此函数可返回两个指定日期间的年份,一年以365.25天为计算单位.其返回值包含小数部分
StartOfAYear
DateUtils
此函数可返回特定年份的第一天
EndOfAYear
DateUtils
此函数可返回特定年份的最后一天
StrtOfTheYear
DateUtils
此函数可返回特定日期的该年第一天
EndOfTheYear
DateUtils
此函数可返回特定日期的该年最后一天
IncYear
DateUtils
此函数可将指定的TDateTime变量加上指定的年度,其默认值为1年
MonthOf
DateUtils
此函数可获取TDateTime格式中的月份,其返回值为Word类型
MonthOfTheYear
DateUtils
此函数可获取TDateTime格式中的月份,其返回值为Word类型
MonthBetween
DateUtils
次函数可返回两个指定日期间的月份数,一个月以30.4375天为计算单位.其返回值不包含小数部分
MonthSpan
DateUtils
次函数可返回两个指定日期间的月份数,一个月以30.4375天为计算单位.其返回值包含小数部分
StartOfAMonth
DateUtils
此函数可返回特定年月的第一天
EndOfAMonth
DateUtils
此函数可返回特定年月的最后一天
StartOfTheMonth
DateUtils
此函数可返回指定日期的该年的第一天
EndOfTheMonth
DateUtils
此函数可返回指定日期的该年的最后一天
IncMonth
DateUtils
此函数可将指定的TDateTime变量加上指定的月份,其默认值为加上1个月
IncAMonth
DateUtils
此函数可将指定的年月日加上指定的月份,其默认值为加上1个月
DaysInAYear
DateUtils
此函数可返回指定年份的总天数
DaysInYear
DateUtils
此函数可返回指定TDateTime变量中该年分的总天数
DaysInAMonth
DateUtils
此函数可返回指定月份的总天数
DaysInMonth
DateUtils
此函数可返回指定TDateTime变量中该月份的总天数
DaysOf
DateUtils
此函数可获取TDateTime格式中的日期,其返回值为Word类型
DaysBetween
DateUtils
此函数可获取格式中的日期,其返回值不包含小数部分
DaySpan
DateUtils
此函数可返回两个指定日期间的天数,其返回值包含小数部分
DayOfTheYear
DateUtils
此函数可返回指定TDateTime变量为该年的第几天.例如2月1日则返回32
DayOfTheMonth
DateUtils
此函数可返回指定TDateTime变量为该月的第几天,其返回值介于1到31
DayOfTheWeek
DateUtils
此函数可返回指定TDateTime变量为该周的第几天,其返回值介于1到7.星期一为第一天.
DayOfWeek
DateUtils
此函数可返回指定TDateTime变量为该周的第几天,其返回值介于1到7.星期日为第一天.
StartOfADay
DateUtils
此函数可返回指定日期一天的开始时间,其返回值为TDateTime类型.其时间默认为12:
00:
000AM
EndOfADay
DateUtils
此函数可返回指定日期一天的结束时间,其返回值为TDateTime类型.其时间默认为11:
59:
999PM
StartOfTheDay
DateUtils
此函数可返回指定TDateTime变量的一天开始时间,其返回值为TDateTime类型.其时间默认为:
12:
00:
000AM
EndOfTheDay
DateUtils
此函数可返回指定TDateTime变量的一天结束时间,其返回值为TDateTime类型.其时间默认为11:
59:
999PM
IncDay
DateUtils
此函数可为指定日期加上特定的天数,其返回值为TDateTime类型,其默认天数为1天
WeeksInAYear
DateUtils
此函数可返回指定年度的周数,其返回值不是52就是53
WeeksInYear
DateUtils
此函数可返回指定TDateTime变量的周数,其返回值不是52就是53
WeekOf
DateUtils
次函数可返回指定日期为该年的第几周,其返回值为1~53
WeekOfTheYear
DateUtils
次函数可返回指定日期为该年的第几周,其返回值为1~53
WeekOfTheMonth
DateUtils
次函数可返回指定日期为该月的第几周,其返回值为1~6
WeeksBetween
DateUtils
此函数可返回两个指定日期间的周数,其返回值不包含小数部分
WeekSpan
DateUtils
此函数可返回两个指定日期间的周数,其返回值包含小数部分
StartOfAweek
DateUtils
此函数可返回指定日期一周的开始时间,其返回值为TDateTime类型.其时间默认为12:
00:
000PM
EndOfAWeek
DateUtils
此函数可返回指定日期一周的结束时间,其返回值为TDateTime类型.其时间默认为11:
59:
999PM
StartOfTheWeek
DateUtils
此函数可返回指定TDateTime变量的一周开始时间,其返回值为TDateTime类型.其其时间默认为12:
00:
000AM
EndOfTheWeek
DateUtils
此函数可返回指定TDateTime变量的一周结束时间,其返回值为TDateTime类型.其时间默认为11:
59:
999AM
IncWeek
DateUtils
此函数可将指定日期加上指定周数,其返回值为TDateTime类型
YearOf(返回指定日期的年度)
引用单元:
DateUtils
函数声明:
FunctionYearOf(constAValue:
TDateTime):
Word;
范例D-9
ProcedureTForm1.Button1Click(Sender:
TObject);
Begin
//三者都相同
Showmessage('年度='+IntToStr(YearOf(Now)));
Showmessage('年度='+IntToStr(YearOf(Date)));
Showmessage('年度='+IntToStr(YearOf(Today)));
end;
YearsBetween(返回两个指定日期间的年份)
引用单元:
DateUtils
函数声明:
FunctionYearsBetween(constANow,AThen:
TDateTime):
Integer;
范例D-10
ProcedureTForm1.Button1Click(Sender:
TObject);
Begin
//不包含小数,一年以365.25天为计算单位
Showmessage('几年='+IntToStr(YearsBetween(Now,Now+560)));//1
end;
YearSpan(返回两个指定日期间的年份)
引用单元:
DateUtils
函数声明:
FunctionYearsSpan(constANow,AThen:
TDateTime):
Double;
范例D-11
ProcedureTForm1.Button1Click(Sender:
TObject);
Begin
//包含小数,一年以365.25天为计算单位
Showmessage('几年='+FloatToStr(YearSpan(Now,Now+560)));
//1.53….
end;
StartOfAYear(返回特定年份的第一天)
引用单元:
DateUtils
函数声明:
FunctionStartOfAYear(constAYear):
TDateTime;
范例D-12
ProcedureTForm1.Button1Click(Sender:
TObject);
Begin
Showmessage(DateTimeToStr(StartOfAYear(2001)));
//2001/1/1早上12:
00:
00
end;
EndOfAYear(返回特定年份的最后一天)
引用单元:
DateUtils
函数声明:
FunctionEndOfAYear(constAYear):
TDateTime;
范例D-13
ProcedureTForm1.Button1Click(Sender:
TObject);
Begin
Showmessage(DateTimeToStr(EndOfAYear(2001)));
//2001/12/31下午11:
59:
59
end;
StartOfTheYear(返回指定日期的该年的第一天)
引用单元:
DateUtils
函数声明:
FunctionStartOfTheYear(constAValue:
TDateTime):
TDateTime;
范例D-14
ProcedureTForm1.Button1Click(Sender:
TObject);
Begin
Showmessage(DateTimeToStr(StarOfTheYear(Now)));
//指定的年度/1/1早上12:
00:
00
end;
EndOfTheYear(返回指定日期的该年的最后一天)
引用单元:
DateUtils
函数声明:
FunctionEndOfTheYear(constAValue:
TDateTime):
TDateTime;
范例D-15
ProcedureTForm1.Button1Click(Sender:
TObject);
Begin
Showmessage(DateTimeToStr(StarOfTheYear(Now)));
//指定年度/12/31下午11:
59:
59
end;
IncYear(将指定的TDateTime变量加上指定的年)
引用单元:
DateUtils
函数声明:
FunctionIncYear(constAValue:
TDateTimel;constANumberOfYears:
Integer=1):
TDateTime;
范例D-16
ProcedureTForm1.Button1Click(Sender:
TObject);
var
myDateTime:
TDateTime;
Begin
myDateTime:
=IncYear(Now,3);//往后加3年
Showmessage(DateTimeToStr(myDateTime));
End;
MonthOf(获取TDateTime格式中的月份)
引用单元:
DateUtils
函数声明:
FunctionMonthOf(constAValue:
TDateTime):
Word;
范例D-17
ProcedureTForm1.Button1Click(Sender:
TObject);
var
myDateTime:
TDateTime;
m:
Integer;
Begin
myDateTime:
=Now;
//两个函数获取相同的结果
m:
=MonthofTheYear(myDateTime);
Showmessage(IntToStr(m));
end;
MonthOfTheYear(获取TDateTime格式中的月份)
引用单元:
DateUtils
函数声明:
FunctionMonthOfTheYear(constAValue:
TDateTime):
Word;
范例D-18
ProcedureTForm1.Button1Click(Sender:
TObject);
var
i:
Integer;
f:
Double;
Begin
i:
=MonthsBetween(Now,Now+89);//差89天
Showmessage(IntToStr(i));//2
f:
=MonthSpan(Now,Now+89);//差89天
Showmessage(FloatToStr(f));//2.924…
End;
MonthsBetween(返回两个指定日期间的月份数)
引用单元:
DateUtils
函数声明:
FunctionMonthsBetween(constANow,AThen:
TDateTime):
Integer;
MonthSpan(返回两个指定日期间的月份数)
引用单元:
DateUtils
函数声明:
FunctionMonthSpan(constANow,AThen:
TDateTime):
Double;
StartOfAMonth(返回特定年月的第一天)
引用单元:
DateUtils
函数声明:
FunctionStartOfAMonth(constAYear,AMonth:
Word):
TDateTime;
EndOfAMonth(返回特定年月的最后一天)
引用单元:
DateUtils
函数声明:
FunctionEndOfTheMonth(constAYear,AMonth:
Word):
TDateTime;
StartOfTheMonth(返回特定年月的第一天)
引用单元:
DateUtils
函数声明:
FunctionStartOfTheMonth(constAValue:
TDateTime):
TDateTime;
EndOfTheMonth(返回特定年月的最后一天)
引用单元:
DateUtils
函数声明:
FunctionEndOfTheMonth(constAValue:
TDateTime):
TDateTime;
范例D-19
var
myDateTime:
TDateTime;
Year,Month:
Integer;
Begin
Year:
=YearOf(Now);
Month:
=MonthOf(Now);
//找出当前月份的第一天
myDateTime:
=StartOfAMonth(Year,Month);
Showmessage(DateTimeToStr(myDateTime));
//找出当前月份的最后一天
myDateTime:
=EndOfAMonth(Year,Month);
Showmessage(DateTimeToStr(myDateTime));
//找出当前月份的第一天
myDateTime:
=StartOfTheMonth(Now);
Showmessage(DateTimeToStr(myDateTime));
//找出当前月份的最后一天
myDateTime:
=EndOfTheMonth(Now);
Showmessage(DateTimeToStr(myDateTime));
end;
IncMonth(指定的TDateTime变量加上指定的月份)
引用单元:
DateUtils
函数声明:
FunctionIncMonth(constDate:
TDateTime;NumberOfMonths:
Integer=1):
TDateTime;
IncAMonth(指定的年月日变量加上指定的月份)
引用单元:
DateUtils
函数声明:
ProcedureIncAMonth(VarYear,Month,Day:
Word;NumberOfMonths:
Integer=1);
范例D-20
var
myDateTime:
TDateTime;