Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

Week Numbers According to ISO8601

5.00/5 (15 votes)
7 Mar 2010CPOL 1  
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.

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

:)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)