Delphi 11.3中从一个日期时间中算出当月(当年、当季)的第一天与最后一天
函数代码 function DateMonthStart(const DT: TDateTime): TDateTime; var Day, Month, Year: Word; begin SysUtils.DecodeDate(DT, Year, Month, Day); Result := SysUtils.EncodeDate(Year, Month, 1); end; function DateMonthEnd(const DT: TDateTime): TDateTime; var Day, Month, Year: Word; LastDay: Byte; begin SysUtils.DecodeDate(DT, Year, Month, Day); LastDay := DaysInMonth(DT); Result := SysUtils.EncodeDate(Year, Month, LastDay); end; function DateQuarterStart(const D: TDateTime): TDateTime; var Year, Month, Day, Quarter: Word; begin SysUtils.DecodeDate(D, Year, Month, Day); Quarter := 4 - ((12 - Month) div 3); Month := 0; SysUtils.IncAMonth(Year, Month, Day, (Quarter * 3) - 2); Result := SysUtils.EncodeDate(Year, Month, 1); end; function DateQuarterEnd(const D: TDateTime): TDateTime; var Year, Month, Day, Quarter: Word; begin SysUtils.DecodeDate(D, Year, Month, Day); Quarter := 4 - ((12 - Month) div 3); // get 1st day of following quarter Month := 0; SysUtils.IncAMonth(Year, Month, Day, Quarter * 3 + 1); // required date is day before 1st day of following quarter Result := SysUtils.EncodeDate(Year, Month, 1) - 1.0; end; function DateYearStart(const DT: TDateTime): TDateTime; var Year, Month, Day: Word; begin SysUtils.DecodeDate(DT, Year, Month, Day); Result := SysUtils.EncodeDate(Year, 1, 1); end; function DateYearEnd(const DT: TDateTime): TDateTime; var Year, Month, Day: Word; begin SysUtils.DecodeDate(DT, Year, Month, Day); Result := SysUtils.EncodeDate(Year, 12, 31); end; 测试 procedure TForm1.Button1Click(Sender: TObject); begin Memo1.Lines.Add(DateTimeToStr(DateMonthStart(Now))) ; Memo1.Lines.Add(DateTimeToStr(DateMonthEnd(Now))) ; Memo1.Lines.Add(DateTimeToStr(DateQuarterStart(Now))) ; Memo1.Lines.Add(DateTimeToStr(DateQuarterEnd(Now))) ; Memo1.Lines.Add(DateTimeToStr(DateYearStart(Now))) ; Memo1.Lines.Add(DateTimeToStr(DateYearEnd(Now))) ; end; ··· ———————————————— 原文链接:https://blog.csdn.net/tjsoft/article/details/134172266
Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号
执行时间: 0.037458896636963 seconds