I think this is the source of what I wrote and the same method somewhat
modified.
"Simen Sandelien says that will
produce results incompatible with ISO 8601"
"He has this to say about that :"
"My conclusion is that the builtin .NET FourDayWeekRule
and the GetWeekOfYear() method do NOT produce
week numbers according to ISO 8601."
private int WeekNumber_Entire4DayWeekRule(DateTime date)
{
const int JAN = 1;
const int DEC = 12;
const int LASTDAYOFDEC = 31;
const int FIRSTDAYOFJAN = 1;
const int THURSDAY = 4;
bool ThursdayFlag = false;
int DayOfYear = date.DayOfYear;
int StartWeekDayOfYear =
(int)(new DateTime(date.Year, JAN, FIRSTDAYOFJAN)).DayOfWeek;
int EndWeekDayOfYear =
(int)(new DateTime(date.Year, DEC, LASTDAYOFDEC)).DayOfWeek;
StartWeekDayOfYear = StartWeekDayOfYear;
EndWeekDayOfYear = EndWeekDayOfYear;
if( StartWeekDayOfYear == 0)
StartWeekDayOfYear = 7;
if( EndWeekDayOfYear == 0)
EndWeekDayOfYear = 7;
int DaysInFirstWeek = 8 - (StartWeekDayOfYear );
int DaysInLastWeek = 8 - (EndWeekDayOfYear );
if (StartWeekDayOfYear == THURSDAY || EndWeekDayOfYear == THURSDAY)
ThursdayFlag = true;
int FullWeeks = (int) Math.Ceiling((DayOfYear -
(DaysInFirstWeek))/7.0);
int WeekNumber = FullWeeks;
if (DaysInFirstWeek >= THURSDAY)
WeekNumber = WeekNumber +1;
if (WeekNumber > 52 && !ThursdayFlag)
WeekNumber = 1;
if (WeekNumber == 0)
WeekNumber = WeekNumber_Entire4DayWeekRule(
new DateTime(date.Year-1, DEC, LASTDAYOFDEC));
return WeekNumber;
}
"TAB" <
[email protected]> skrev i meddelandet
Hi Tommy
I had the same problem when I was working with a calendarprogram and
found
this somewhere on the Internet.
Apperently there is an old bug .Net that hasn't be solved yet.
This is working for me, I have checked several years back and forth.
// get week number for current date
public int WeekNumber(DateTime fromDate)
{
// Get jan 1st of the year
DateTime startOfYear = fromDate.AddDays(-fromDate.Day +
1).AddMonths(-fromDate.Month + 1);
// Get dec 31st of the year
DateTime endOfYear = startOfYear.AddYears(1).AddDays(-1);
// ISO 8601 weeks start with Monday
// The first week of a year includes the first Thursday, i.e.
at least 4 days
// DayOfWeek returns 0 for sunday up to 6 for Saturday
int[] iso8601Correction = { 6, 7, 8, 9, 10, 4, 5 };
int nds = fromDate.Subtract(startOfYear).Days +
iso8601Correction[(int)startOfYear.DayOfWeek];
int wk = nds / 7;
switch (wk)
{
case 0:
// Return weeknumber of dec 31st of the previous year
return WeekNumber(startOfYear.AddDays(-1));
case 53:
// If dec 31st falls before thursday it is week 01 of
next year
if (endOfYear.DayOfWeek < DayOfWeek.Thursday)
return 1;
else
return wk;
default: return wk;
}
"Tommy Jakobsen" <
[email protected]> skrev i meddelandet
Hi Clint.
That doesn't work.
Theres an error in the framework. Using date DateTime(2008, 12, 31)
returns 53,
but theres only 52 weeks in year 2008.
Any idea?
On Tue, 16 Sep 2008 06:55:01 -0700, Morten Wennevik [C# MVP]
:
Is there something special about the Danish calendar? i.e. is the
answer not
always 52? (plus a couple of days)
Hi.
Is there a method in .NET that takes "year" as an argument and
returns the
total
number of weeks in that year? For culture da-DK (Danish).
Thanks in advance.
Tommy.
Sometimes there are 53 weeks in a year.
int year = 2004;
DateTime dt = new DateTime(year, 12, 31);
int week =
CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(
dt,
CalendarWeekRule.FirstFourDayWeek,
DayOfWeek.Monday);
// week == 53