📄 dateutils.pp
字号:
(ADayOfYear<=DaysPerYear[IsLeapYear(AYear)]);end;Function IsValidDateWeek(const AYear, AWeekOfYear, ADayOfWeek: Word): Boolean;begin Result:=(AYear<>0) and (AYear<10000) and (ADayOfWeek in [1..7]) and (AWeekOfYear<>0) and (AWeekOfYear<=WeeksInaYear(AYear)); { should we not also check whether the day of the week is not larger than the last day of the last week in the year 9999 ?? }end;Function IsValidDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word): Boolean;begin Result:=(AYear<>0) and (AYear<10000) and (AMonth in [1..12]) and (AWeekOfMonth in [1..5]) and (ADayOfWeek in [1..7]);end;{ --------------------------------------------------------------------- Enumeration functions. ---------------------------------------------------------------------}Function WeeksInYear(const AValue: TDateTime): Word;Var Y,M,D : Word;begin DecodeDate(AValue,Y,M,D); Result:=WeeksInAYear(Y);end;Function WeeksInAYear(const AYear: Word): Word;Var DOW : Word;begin Result:=52; DOW:=DayOfTheWeek(StartOfAYear(AYear)); If (DOW=4) or ((DOW=3) and IsLeapYear(AYear)) then Inc(Result);end;Function DaysInYear(const AValue: TDateTime): Word;Var Y,M,D : Word;begin DecodeDate(AValue,Y,M,D); Result:=DaysPerYear[IsLeapYear(Y)];end;Function DaysInAYear(const AYear: Word): Word;begin Result:=DaysPerYear[Isleapyear(AYear)];end;Function DaysInMonth(const AValue: TDateTime): Word;Var Y,M,D : Word;begin Decodedate(AValue,Y,M,D); Result:=MonthDays[IsLeapYear(Y),M];end;Function DaysInAMonth(const AYear, AMonth: Word): Word;begin Result:=MonthDays[IsLeapYear(AYear),AMonth];end;{ --------------------------------------------------------------------- Variations on current date/time. ---------------------------------------------------------------------}Function Today: TDateTime;begin Result:=Date;end;Function Yesterday: TDateTime;begin Result:=Date-1;end;Function Tomorrow: TDateTime;begin Result:=Date+1;end;Function IsToday(const AValue: TDateTime): Boolean;begin Result:=IsSameDay(AValue,Date);end;Function IsSameDay(const AValue, ABasis: TDateTime): Boolean;Var D : TDateTime;begin D:=AValue-Trunc(ABasis); Result:=(D>=0) and (D<1);end;const DOWMap: array [1..7] of Word = (7, 1, 2, 3, 4, 5, 6);Function PreviousDayOfWeek (DayOfWeek : Word) : Word;begin If Not (DayOfWeek in [1..7]) then Raise EConvertError.CreateFmt(SErrInvalidDayOfWeek,[DayOfWeek]); Result:=DOWMap[DayOfWeek];end;{ --------------------------------------------------------------------- Extraction functions. ---------------------------------------------------------------------}Function YearOf(const AValue: TDateTime): Word;Var D,M : Word;begin DecodeDate(AValue,Result,D,M);end;Function MonthOf(const AValue: TDateTime): Word;Var Y,D : Word;begin DecodeDate(AValue,Y,Result,D);end;Function WeekOf(const AValue: TDateTime): Word;begin Result:=WeekOfTheYear(AValue);end;Function DayOf(const AValue: TDateTime): Word;Var Y,M : Word;begin DecodeDate(AValue,Y,M,Result);end;Function HourOf(const AValue: TDateTime): Word;Var N,S,MS : Word;begin DecodeTime(AValue,Result,N,S,MS);end;Function MinuteOf(const AValue: TDateTime): Word;Var H,S,MS : Word;begin DecodeTime(AValue,H,Result,S,MS);end;Function SecondOf(const AValue: TDateTime): Word;Var H,N,MS : Word;begin DecodeTime(AVAlue,H,N,Result,MS);end;Function MilliSecondOf(const AValue: TDateTime): Word;Var H,N,S : Word;begin DecodeTime(AValue,H,N,S,Result);end;{ --------------------------------------------------------------------- Start/End of year functions. ---------------------------------------------------------------------}Function StartOfTheYear(const AValue: TDateTime): TDateTime;Var Y,M,D : Word;begin DecodeDate(AValue,Y,M,D); Result:=EncodeDate(Y,1,1);end;Function EndOfTheYear(const AValue: TDateTime): TDateTime;Var Y,M,D : Word;begin DecodeDate(AValue,Y,M,D); Result:=EncodeDateTime(Y,12,31,23,59,59,999);end;Function StartOfAYear(const AYear: Word): TDateTime;begin Result:=EncodeDate(AYear,1,1);end;Function EndOfAYear(const AYear: Word): TDateTime;begin Result:=(EncodeDateTime(AYear,12,31,23,59,59,999));end;{ --------------------------------------------------------------------- Start/End of month functions. ---------------------------------------------------------------------}Function StartOfTheMonth(const AValue: TDateTime): TDateTime;Var Y,M,D : Word;begin DecodeDate(AValue,Y,M,D); Result:=EncodeDate(Y,M,1);// MonthDays[IsLeapYear(Y),M])end;Function EndOfTheMonth(const AValue: TDateTime): TDateTime;Var Y,M,D : Word;begin DecodeDate(AValue,Y,M,D); Result:=EncodeDateTime(Y,M,MonthDays[IsLeapYear(Y),M],23,59,59,999);end;Function StartOfAMonth(const AYear, AMonth: Word): TDateTime;begin Result:=EncodeDate(AYear,AMonth,1);end;Function EndOfAMonth(const AYear, AMonth: Word): TDateTime;begin Result:=EncodeDateTime(AYear,AMonth,MonthDays[IsLeapYear(AYear),AMonth],23,59,59,999);end;{ --------------------------------------------------------------------- Start/End of week functions. ---------------------------------------------------------------------}Function StartOfTheWeek(const AValue: TDateTime): TDateTime;begin Result:=Trunc(AValue)-DayOfTheWeek(AValue)+1;end;Function EndOfTheWeek(const AValue: TDateTime): TDateTime;begin Result:=EndOfTheDay(AValue-DayOfTheWeek(AValue)+7);end;Function StartOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;begin Result:=EncodeDateWeek(AYear,AWeekOfYear,ADayOfWeek);end;Function StartOfAWeek(const AYear, AWeekOfYear: Word): TDateTime; // ADayOFWeek 1begin Result:=StartOfAWeek(AYear,AWeekOfYear,1)end;Function EndOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;begin Result := EndOfTheDay(EncodeDateWeek(AYear, AWeekOfYear, ADayOfWeek));end;Function EndOfAWeek(const AYear, AWeekOfYear: Word): TDateTime; // const ADayOfWeek: Word = 7begin Result:=EndOfAWeek(AYear,AWeekOfYear,7);end;{ --------------------------------------------------------------------- Start/End of day functions. ---------------------------------------------------------------------}Function StartOfTheDay(const AValue: TDateTime): TDateTime;begin StartOfTheDay:=Trunc(Avalue);end;Function EndOfTheDay(const AValue: TDateTime): TDateTime;Var Y,M,D : Word;begin DecodeDate(AValue,Y,M,D); Result:=EncodeDateTime(Y,M,D,23,59,59,999);end;Function StartOfADay(const AYear, AMonth, ADay: Word): TDateTime;begin Result:=EncodeDate(AYear,AMonth,ADay);end;Function StartOfADay(const AYear, ADayOfYear: Word): TDateTime;begin Result:=StartOfAYear(AYear)+ADayOfYear;end;Function EndOfADay(const AYear, AMonth, ADay: Word): TDateTime;begin Result:=EndOfTheDay(EncodeDate(AYear,AMonth,ADay));end;Function EndOfADay(const AYear, ADayOfYear: Word): TDateTime;begin Result:=StartOfAYear(AYear)+ADayOfYear+EncodeTime(23,59,59,999);end;{ --------------------------------------------------------------------- Part of year functions. ---------------------------------------------------------------------}Function MonthOfTheYear(const AValue: TDateTime): Word;Var Y,D : Word;begin DecodeDate(AValue,Y,Result,D);end;Function WeekOfTheYear(const AValue: TDateTime): Word;Var Y,DOW : Word;begin DecodeDateWeek(AValue,Y,Result,DOW)end;Function WeekOfTheYear(const AValue: TDateTime; var AYear: Word): Word;Var DOW : Word;begin DecodeDateWeek(AValue,AYear,Result,DOW);end;Function DayOfTheYear(const AValue: TDateTime): Word;begin Result:=Trunc(AValue-StartOfTheYear(AValue)+1);end;Function HourOfTheYear(const AValue: TDateTime): Word;Var H,M,S,MS : Word;begin DecodeTime(AValue,H,M,S,MS); Result:=H+((DayOfTheYear(AValue)-1)*24);end;Function MinuteOfTheYear(const AValue: TDateTime): LongWord;Var H,M,S,MS : Word;begin DecodeTime(AValue,H,M,S,MS); Result:=M+(H+((DayOfTheYear(AValue)-1)*24))*60;end;Function SecondOfTheYear(const AValue: TDateTime): LongWord;Var H,M,S,MS : Word;begin DecodeTime(AValue,H,M,S,MS); Result:=(M+(H+((DayOfTheYear(AValue)-1)*24))*60)*60+S;end;Function MilliSecondOfTheYear(const AValue: TDateTime): Int64;Var H,M,S,MS : Word;begin DecodeTime(AValue,H,M,S,MS); Result:=((M+(H+((DayOfTheYear(AValue)-1)*24))*60)*60+S)*1000+MS;end;{ --------------------------------------------------------------------- Part of month functions. ---------------------------------------------------------------------}Function WeekOfTheMonth(const AValue: TDateTime): Word;var Y,M,DOW : word;begin DecodeDateMonthWeek(AValue,Y,M,Result,DOW);end;Function WeekOfTheMonth(const AValue: TDateTime; var AYear, AMonth: Word): Word;Var DOW : Word;begin DecodeDateMonthWeek(AValue,AYear,AMonth,Result,DOW);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -