65.9K
CodeProject is changing. Read more.
Home

Week Numbers According to ISO8601

starIconstarIconstarIconstarIconstarIcon

5.00/5 (15 votes)

Mar 7, 2010

CPOL
viewsIcon

20716

This is an alternative to "Week Numbers According to ISO8601".

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[^].

:)