The function NthDayOfWeek returns the weekday of the specified TDateTime value entered as AsValue.

For example, if AValue represents the second Tuesday of the month, NthDayOfWeek returns 2.

This value may differ from the value that the WeekOfTheMonth function returns.

NthDayOfWeek counts every occurrence of the given weekday, while WeekOfTheMonth only counts a week if it includes four or more days in the month.

So if AValue represents a Saturday that is the first day of a month, NthDayOfWeek returns 1, while WeekOfTheMonth returns 5 (or maybe 4), indicating the last week of the previous month.

Please also refer to WeekOfTheMonth.

Declaration: function NthDayOfWeek(const AValue: TDateTime): Word;

This example uses September 2023 as the month.

procedure OnMapEvent(var Value:Variant);
var
TheDate : TDateTime;
TheYear : word;
begin
//1st Friday of 1st Sep 23, so returns 1
TheDate := EncodeDateTime(2023, 09, 01, 14, 25, 15, 650);
TheYear := NthDayOfWeek(TheDate);
LogInfo('The week of the month is '+IntToStr(TheYear));
//3rd Friday of 15th Sep 23, so returns 3
TheDate := EncodeDateTime(2023, 09, 15, 14, 25, 15, 650);
TheYear := NthDayOfWeek(TheDate);
LogInfo('The week of the month is '+IntToStr(TheYear));
end;