ISO 8601 week numbers
To find week number from a given date we can start with finding the week number of the last day of the year and calculate backwards. The last day of the year can be in week 52, 53 or week 1 of the following year. We need 6 rules to calculate week number of a given 31. of december:
If not a leap year, the rules are:
If 31/12 is friday, saturday or sunday then 31/12 is in week 52
If 31/12 is thursday then 31/12 is in week 53
If 31/12 is monday, tuesday or wednesday then 31/12 is in week 1
If leap year, the rules are:
If 31/12 is saturday or sunday then 31/12 is in week 52
If 31/12 is thursday or friday then 31/12 is in week 53
If 31/12 is monday, tuesday or wednesday then 31/12 is in week 1
Here are 6 year end examples:
2001
WK Mo Tu We Th Fr Sa Su
48 . . . . . 1 2
49 3 4 5 6 7 8 9
50 10 11 12 13 14 15 16
51 17 18 19 20 21 22 23
52 24 25 26 27 28 29 30
1 31 . . . . . .
2009
WK Mo Tu We Th Fr Sa Su
49 . 1 2 3 4 5 6
50 7 8 9 10 11 12 13
51 14 15 16 17 18 19 20
52 21 22 23 24 25 26 27
53 28 29 30 31 . . .
2010
WK Mo Tu We Th Fr Sa Su
48 . . 1 2 3 4 5
49 6 7 8 9 10 11 12
50 13 14 15 16 17 18 19
51 20 21 22 23 24 25 26
52 27 28 29 30 31 . .
2008
WK Mo Tu We Th Fr Sa Su
49 1 2 3 4 5 6 7
50 8 9 10 11 12 13 14
51 15 16 17 18 19 20 21
52 22 23 24 25 26 27 28
1 29 30 31 . . . .
2004
WK Mo Tu We Th Fr Sa Su
49 . . 1 2 3 4 5
50 6 7 8 9 10 11 12
51 13 14 15 16 17 18 19
52 20 21 22 23 24 25 26
53 27 28 29 30 31 . .
2000
WK Mo Tu We Th Fr Sa Su
48 . . . . 1 2 3
49 4 5 6 7 8 9 10
50 11 12 13 14 15 16 17
51 18 19 20 21 22 23 24
52 25 26 27 28 29 30 31
To calculate week number for a given date we need to find the date and week number of the last monday:
If 31/12 is in week 52 or 53 then last monday is 31/12 minus week day number of 31/12 (starting with monday as day 0)
If 31/12 is in week 1 then last monday is 31/12 minus one week minus day number of 31/12. Last monday is always in week 52 in this case.
To calculate week number for a given date:
Find number of days between last monday and given date
Find number of weeks between last monday and given date
Subtract number of weeks from week of last monday
If we end up with week 0, week number is equal to week number of 31/12 for previous year
Code for calculating week number:
public class IsoDate1
{
public DateTime date;
public int Year;
public int Week;
public void isoWeek()
{
DateTime newYear = new DateTime(date.Year, 12, 31);
int NewYearWeek;
DateTime lastMonday;
int lastMondayWeek;
int daysBetween;
int weeksBetween;
int dayOfWeek;
if (DateTime.IsLeapYear(date.Year))
{
if (newYear.DayOfWeek == DayOfWeek.Saturday
|| newYear.DayOfWeek == DayOfWeek.Sunday)
NewYearWeek = 52;
else if (newYear.DayOfWeek == DayOfWeek.Friday
|| newYear.DayOfWeek == DayOfWeek.Thursday)
NewYearWeek = 53;
else
NewYearWeek = 1;
}
else
{
if (newYear.DayOfWeek == DayOfWeek.Friday
|| newYear.DayOfWeek == DayOfWeek.Saturday
|| newYear.DayOfWeek == DayOfWeek.Sunday)
NewYearWeek = 52;
else if (newYear.DayOfWeek == DayOfWeek.Thursday)
NewYearWeek = 53;
else
NewYearWeek = 1;
}
if (date == newYear)
{
Week = NewYearWeek;
if (Week == 1)
Year = newYear.Year + 1;
else
Year = newYear.Year;
return;
}
if (NewYearWeek == 1)
{
dayOfWeek = (int)newYear.DayOfWeek - 1;
if (dayOfWeek == -1)
dayOfWeek = 6;
lastMonday = newYear.AddDays(-7 - dayOfWeek);
lastMondayWeek = 52;
}
else
{
dayOfWeek = (int)newYear.DayOfWeek - 1;
if (dayOfWeek == -1)
dayOfWeek = 6;
lastMonday = newYear.AddDays(-dayOfWeek);
lastMondayWeek = NewYearWeek;
}
if (date <= lastMonday)
{
daysBetween = (lastMonday - date).Days;
if (daysBetween == 0)
{
Week = lastMondayWeek;
Year = lastMonday.Year;
}
else
{
weeksBetween = (daysBetween - 1) / 7;
Week = lastMondayWeek - weeksBetween - 1;
if (Week > 0)
Year = date.Year;
else
{
IsoDate1 lastNewYear = new IsoDate1();
lastNewYear.date =
new DateTime(date.Year - 1, 12, 31);
lastNewYear.isoWeek();
Week = lastNewYear.Week;
Year = lastNewYear.Year;
}
}
}
else
{
daysBetween = (date - lastMonday).Days;
if (daysBetween < 7)
{
Week = lastMondayWeek;
Year = lastMonday.Year;
}
else
{
Week = NewYearWeek;
if (Week == 1)
Year = newYear.Year + 1;
else
Year = newYear.Year;
}
}
}
}