TryEncodeDayOfWeekInMonth
The TryEncodeDayOfWeekInMonth function calculates the TDateTime that represents a specified instance of a day ADayOfWeek, of a specified month AMonth, in a specified year AYear, and tests whether a valid TDateTime value would be generated from the specified individual parts.
The time portion of the return value is 00:00:00 - midnight at the start of the specified day ADayOfWeek.
The definition for ADayOfWeek follow the ISO 8601 standard.
AYear is the year which can be any value from 1 through 9999 inclusive.
AMonth can be any month value from 1 through to 12 inclusive.
ANthDayOfWeek is the occurrence of the specified day of the week that is specified by ADayOfWeek, and can be a number from 1 to 5 inclusive, indicating the week within the specified month.
If a month does not include 5 Mondays, setting ANthDayOfWeek to 5 and ADayOfWeek to 1 causes TryEncodeDayOfWeekInMonth to return False.
ADayOfWeek is the day of the week, where 1 is Monday, 2 is Tuesday e.t.c., with 7 representing Sunday.
Please be aware that this function does not correctly interpret ADayOfWeek and will accept values greater than 7. So please ensure that the integer range that is passed in to this parameter is between 1 and 7 inclusive only.
TryEncodeDayOfWeekInMonth returns True if the parameters AYear, AMonth, ANthDayOfWeek, and ADayOfWeek represent a valid date, and False if this combination does not generate a valid TDateTime date.
Declaration: function TryEncodeDayOfWeekInMonth(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word; out AValue: TDateTime): Boolean;
procedure
OnMapEvent(
var
Value:Variant);
var
aYear, aMonth, aNth, aDay :
word
;
xDate, xDate1 : TDateTime;
aGo :
boolean
;
begin
//Create a date/time from its separate parts - returns 10-05-2024 00:00:00:0000
aYear :=
2024
;
aMonth :=
5
aNth :=
2
;
aDay :=
5
;
aGo := TryEncodeDayOfWeekInMonth(aYear, aMonth, aNth, aDay,xDate);
If
aGo =
True
then
LogInfo(
'The date works - '
+FormatDateTime(
'dd-mm-yyyy hh:nn:ss.zzz'
, xDate))
else
LogInfo(
'The date DOES NOT work - '
+FormatDateTime(
'dd-mm-yyyy hh:nn:ss.zzz'
, xDate));
//This test will fail
aYear :=
2023
;
aMonth :=
7
;
aWeek :=
7
;
aDay :=
6
;
aGo := TryEncodeDayOfWeekInMonth(aYear, aMonth, aNth, aDay,xDate1);
If
aGo =
True
then
LogInfo(
'The date works - '
+FormatDateTime(
'dd-mm-yyyy hh:nn:ss.zzz'
, xDate1))
else
LogInfo(
'The date DOES NOT work - '
+FormatDateTime(
'dd-mm-yyyy hh:nn:ss.zzz'
, xDate1));
end
;