Bootstrap

Delphi中提取某个日期中的年、季、月、日

应用场景

经常遇到需要从一个日期时间中提取年、月、日,三个函数搞定。

函数代码

function DateYear(const ADate: TDateTime): Word;
var
  Month, Day: Word; 
begin
  DecodeDate(ADate, Result, Month, Day);
end;

function DateQuarter(const D: TDateTime): Byte;
var
  Year, Month, Day: Word; // year, month and date components of D
begin
  DecodeDate(D, Year, Month, Day);
  Result := 4 - ((12 - Month) div 3);
end;

function DateMonth(const ADate: TDateTime): Word;
var
  Year, Day: Word; 
begin
  DecodeDate(ADate, Year, Result, Day);
end;

function DateDay(const ADate: TDateTime): Word;
var
  Year, Month: Word; 
begin
  DecodeDate(ADate, Year, Month, Result);
end;
;