delphi日期的使用

delphi中日期的函数大部分都在DateUtil中。

1、获取当年当月当周的第一天和最后一天 函数

uses DateUtils;

function StartOfTheYear(const AValue: TDateTime): TDateTime;  //获取当年的第一天
function EndOfTheYear(const AValue: TDateTime): TDateTime;
function StartOfAYear(const AYear: Word): TDateTime;
function EndOfAYear(const AYear: Word): TDateTime;

function StartOfTheMonth(const AValue: TDateTime): TDateTime;
function EndOfTheMonth(const AValue: TDateTime): TDateTime;
function StartOfAMonth(const AYear, AMonth: Word): TDateTime;
function EndOfAMonth(const AYear, AMonth: Word): TDateTime;

function StartOfTheWeek(const AValue: TDateTime): TDateTime;           {ISO 8601}
function EndOfTheWeek(const AValue: TDateTime): TDateTime;             {ISO 8601}
function StartOfAWeek(const AYear, AWeekOfYear: Word;                  {ISO 8601}
   const ADayOfWeek: Word = 1): TDateTime;
function EndOfAWeek(const AYear, AWeekOfYear: Word;                    {ISO 8601}
   const ADayOfWeek: Word = 7): TDateTime;

2、Example

获取年月日期
procedure TForm1.FormCreate(Sender: TObject);
var
dt: TDateTime;
d: TDate;
t: TTime;
y: Word;
begin
dt := Now;
d := Date;
t := Time;
y := CurrentYear;

ShowMessage(DateTimeToStr(dt)); //2009-5-21 17:45:50
ShowMessage(DateToStr(d)); //2009-5-21
ShowMessage(TimeToStr(t)); //17:45:50
ShowMessage(IntToStr(y)); //2009
end;

原文地址:https://www.cnblogs.com/rongxiaoya/p/2882011.html