The code snippet below returns the first Thursday
of a given year without iterating anything.
DateTime FirstThursday(int year) {
DateTime dt=new DateTime(year, 1, 1);
return dt.AddDays((11-(int)dt.DayOfWeek)%7);
}
The formula used may seem a bit bizarre, it computes the distance between first of January
and first Thursday of January
, where DayOfWeek.Thursday=4
, and another week is added to avoid negative numbers; and eventually a modulo 7 operation fixes everything.
More such stuff can be found here[^].
:)