473,883 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Number of weeks in year?

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.
Sep 16 '08
14 19087
That method returns the same as the previous method, doesn't it?

It's still not what I'm looking for.

On Tue, 16 Sep 2008 16:44:03 +0200, "TAB" <an************ *@email.comwrot e:
>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_Enti re4DayWeekRule( 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 StartWeekDayOfY ear =
(int)(new DateTime(date.Y ear, JAN, FIRSTDAYOFJAN)) .DayOfWeek;
int EndWeekDayOfYea r =
(int)(new DateTime(date.Y ear, DEC, LASTDAYOFDEC)). DayOfWeek;

StartWeekDayOfY ear = StartWeekDayOfY ear;
EndWeekDayOfYea r = EndWeekDayOfYea r;
if( StartWeekDayOfY ear == 0)
StartWeekDayOfY ear = 7;
if( EndWeekDayOfYea r == 0)
EndWeekDayOfYea r = 7;

int DaysInFirstWeek = 8 - (StartWeekDayOf Year );
int DaysInLastWeek = 8 - (EndWeekDayOfYe ar );

if (StartWeekDayOf Year == THURSDAY || EndWeekDayOfYea r == THURSDAY)
ThursdayFlag = true;

int FullWeeks = (int) Math.Ceiling((D ayOfYear -
(DaysInFirstWe ek))/7.0);

int WeekNumber = FullWeeks;

if (DaysInFirstWee k >= THURSDAY)
WeekNumber = WeekNumber +1;

if (WeekNumber 52 && !ThursdayFlag)
WeekNumber = 1;

if (WeekNumber == 0)
WeekNumber = WeekNumber_Enti re4DayWeekRule(
new DateTime(date.Y ear-1, DEC, LASTDAYOFDEC));
return WeekNumber;
}

"TAB" <an************ *@email.comskre v i meddelandet
news:e9******* *******@TK2MSFT NGP02.phx.gbl.. .
>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(Date Time fromDate)
{
// Get jan 1st of the year
DateTime startOfYear = fromDate.AddDay s(-fromDate.Day +
1).AddMonths (-fromDate.Month + 1);
// Get dec 31st of the year
DateTime endOfYear = startOfYear.Add Years(1).AddDay s(-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[] iso8601Correcti on = { 6, 7, 8, 9, 10, 4, 5 };
int nds = fromDate.Subtra ct(startOfYear) .Days +
iso8601Correct ion[(int)startOfYea r.DayOfWeek];
int wk = nds / 7;
switch (wk)
{
case 0:
// Return weeknumber of dec 31st of the previous year
return WeekNumber(star tOfYear.AddDays (-1));
case 53:
// If dec 31st falls before thursday it is week 01 of
next year
if (endOfYear.DayO fWeek < DayOfWeek.Thurs day)
return 1;
else
return wk;
default: return wk;
}

"Tommy Jakobsen" <to***@holmjako bsen.dkskrev i meddelandet
news:n0******* *************** **********@4ax. com...
>>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]
<Mo********** **@hotmail.comw rote:
"Clint" wrote:

Is there something special about the Danish calendar? i.e. is the
answer not
always 52? (plus a couple of days)
>
"Tommy Jakobsen" <to***@holmjako bsen.dkwrote in message
news:1m**** *************** *************@4 ax.com...
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.Cur rentCulture.Cal endar.GetWeekOf Year(
dt,
CalendarWeekRul e.FirstFourDayW eek,
DayOfWeek.Monda y);
// week == 53
Sep 16 '08 #11
TAB
Start with the last day of the year and subtract on day at a time until you
find the previous week, whatever it may be.
"Tommy Jakobsen" <to***@holmjako bsen.dkskrev i meddelandet
news:44******** *************** *********@4ax.c om...
That method returns the same as the previous method, doesn't it?

It's still not what I'm looking for.

On Tue, 16 Sep 2008 16:44:03 +0200, "TAB" <an************ *@email.com>
wrote:
>>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_Enti re4DayWeekRule( 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 StartWeekDayOfY ear =
(int)(new DateTime(date.Y ear, JAN, FIRSTDAYOFJAN)) .DayOfWeek;
int EndWeekDayOfYea r =
(int)(new DateTime(date.Y ear, DEC, LASTDAYOFDEC)). DayOfWeek;

StartWeekDayOfY ear = StartWeekDayOfY ear;
EndWeekDayOfYea r = EndWeekDayOfYea r;
if( StartWeekDayOfY ear == 0)
StartWeekDayOfY ear = 7;
if( EndWeekDayOfYea r == 0)
EndWeekDayOfYea r = 7;

int DaysInFirstWeek = 8 - (StartWeekDayOf Year );
int DaysInLastWeek = 8 - (EndWeekDayOfYe ar );

if (StartWeekDayOf Year == THURSDAY || EndWeekDayOfYea r == THURSDAY)
ThursdayFlag = true;

int FullWeeks = (int) Math.Ceiling((D ayOfYear -
(DaysInFirstW eek))/7.0);

int WeekNumber = FullWeeks;

if (DaysInFirstWee k >= THURSDAY)
WeekNumber = WeekNumber +1;

if (WeekNumber 52 && !ThursdayFlag)
WeekNumber = 1;

if (WeekNumber == 0)
WeekNumber = WeekNumber_Enti re4DayWeekRule(
new DateTime(date.Y ear-1, DEC, LASTDAYOFDEC));
return WeekNumber;
}

"TAB" <an************ *@email.comskre v i meddelandet
news:e9****** ********@TK2MSF TNGP02.phx.gbl. ..
>>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(Date Time fromDate)
{
// Get jan 1st of the year
DateTime startOfYear = fromDate.AddDay s(-fromDate.Day +
1).AddMonth s(-fromDate.Month + 1);
// Get dec 31st of the year
DateTime endOfYear = startOfYear.Add Years(1).AddDay s(-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[] iso8601Correcti on = { 6, 7, 8, 9, 10, 4, 5 };
int nds = fromDate.Subtra ct(startOfYear) .Days +
iso8601Correc tion[(int)startOfYea r.DayOfWeek];
int wk = nds / 7;
switch (wk)
{
case 0:
// Return weeknumber of dec 31st of the previous year
return WeekNumber(star tOfYear.AddDays (-1));
case 53:
// If dec 31st falls before thursday it is week 01 of
next year
if (endOfYear.DayO fWeek < DayOfWeek.Thurs day)
return 1;
else
return wk;
default: return wk;
}

"Tommy Jakobsen" <to***@holmjako bsen.dkskrev i meddelandet
news:n0****** *************** ***********@4ax .com...
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]
<Mo********* ***@hotmail.com wrote:

>
>"Clint" wrote:
>
>Is there something special about the Danish calendar? i.e. is the
>answer not
>always 52? (plus a couple of days)
>>
>"Tommy Jakobsen" <to***@holmjako bsen.dkwrote in message
>news:1m*** *************** **************@ 4ax.com...
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.
>>
>
>Sometime s there are 53 weeks in a year.
>
int year = 2004;
DateTime dt = new DateTime(year, 12, 31);
int week =
CultureInfo .CurrentCulture .Calendar.GetWe ekOfYear(
dt,
CalendarWeekRul e.FirstFourDayW eek,
DayOfWeek.Monda y);
>
>
// week == 53
Sep 16 '08 #12
On Sep 16, 10:54*am, Tommy Jakobsen <to...@holmjako bsen.dkwrote:
That method returns the same as the previous method, doesn't it?

It's still not what I'm looking for.

On Tue, 16 Sep 2008 16:44:03 +0200, "TAB" <anders.bergl.. .@email.comwrot e:
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_Enti re4DayWeekRule( 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 StartWeekDayOfY ear =
* * * * *(int)(new DateTime(date.Y ear, JAN, FIRSTDAYOFJAN)) .DayOfWeek;
* * int EndWeekDayOfYea r =
* * * * *(int)(new DateTime(date.Y ear, DEC, LASTDAYOFDEC)). DayOfWeek;
* * StartWeekDayOfY ear = StartWeekDayOfY ear;
* * EndWeekDayOfYea r = EndWeekDayOfYea r;
* * if( StartWeekDayOfY ear == 0)
* * * * *StartWeekDayOf Year = 7;
* * if( EndWeekDayOfYea r == 0)
* * * * *EndWeekDayOfYe ar = 7;
* * int DaysInFirstWeek = 8 - (StartWeekDayOf Year *);
* * int DaysInLastWeek = 8 - (EndWeekDayOfYe ar );
* * if (StartWeekDayOf Year == THURSDAY || EndWeekDayOfYea r == THURSDAY)
* * * * *ThursdayFlag = true;
* * int FullWeeks = (int) Math.Ceiling((D ayOfYear -
(DaysInFirstWee k))/7.0);
* * int WeekNumber = FullWeeks;
* * if (DaysInFirstWee k >= THURSDAY)
* * * * *WeekNumber = WeekNumber +1;
* * if (WeekNumber 52 && !ThursdayFlag)
* * * * *WeekNumber = 1;
* * if (WeekNumber == 0)
* * * * *WeekNumber = WeekNumber_Enti re4DayWeekRule(
* * * * * * * new DateTime(date.Y ear-1, DEC, LASTDAYOFDEC));
* * return WeekNumber;
}
"TAB" <anders.bergl.. .@email.comskre v i meddelandet
news:e9******** ******@TK2MSFTN GP02.phx.gbl...
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(Date Time fromDate)
* * * *{
* * * * * *// Get jan 1st of the year
* * * * * *DateTime startOfYear = fromDate.AddDay s(-fromDate.Day +
1).AddMonths(-fromDate.Month + 1);
* * * * * *// Get dec 31st of the year
* * * * * *DateTime endOfYear = startOfYear.Add Years(1).AddDay s(-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[] iso8601Correcti on = { 6, 7, 8, 9, 10, 4, 5 };
* * * * * *int nds = fromDate.Subtra ct(startOfYear) .Days+
iso8601Correcti on[(int)startOfYea r.DayOfWeek];
* * * * * *int wk = nds / 7;
* * * * * *switch (wk)
* * * * * *{
* * * * * * * *case 0:
* * * * * * * * * *// Return weeknumber of dec 31st of the previous year
* * * * * * * * * *return WeekNumber(star tOfYear.AddDays (-1));
* * * * * * * *case 53:
* * * * * * * * * *// If dec 31st falls before thursday it is week 01 of
next year
* * * * * * * * * *if (endOfYear.DayO fWeek < DayOfWeek.Thurs day)
* * * * * * * * * * * *return 1;
* * * * * * * * * *else
* * * * * * * * * * * *return wk;
* * * * * * * *default: return wk;
* * * * * *}
"Tommy Jakobsen" <to...@holmjako bsen.dkskrev i meddelandet
news:n0******* *************** **********@4ax. com...
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]
<MortenWenne.. .@hotmail.comwr ote:
>>>"Clint" wrote:
>>>Is there something special about the Danish calendar? i.e. is the
answer not
always 52? (plus a couple of days)
>>>"Tommy Jakobsen" <to...@holmjako bsen.dkwrote in message
news:1m**** *************** *************@4 ax.com...
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.Cur rentCulture.Cal endar.GetWeekOf Year(
* * * * * * * *dt,
* * * * * * * *CalendarWeekRu le.FirstFourDay Week,
* * * * * * * *DayOfWeek.Mond ay);
>>* * * * * *// week == 53- Hide quoted text -

- Show quoted text -
I'd echo firts responder's question - what do you consider the first
week?

The example in this article should clarify things a bit:
http://msdn.microsoft.com/en-us/libr...le(VS.95).aspx
Sep 16 '08 #13
I got it modified to take year as parameter and return the total number of weeks
(52/53) in that year.

Thanks for your replies guys.

On Tue, 16 Sep 2008 17:18:20 +0200, "TAB" <an************ *@email.comwrot e:
>Start with the last day of the year and subtract on day at a time until you
find the previous week, whatever it may be.
"Tommy Jakobsen" <to***@holmjako bsen.dkskrev i meddelandet
news:44******* *************** **********@4ax. com...
>That method returns the same as the previous method, doesn't it?

It's still not what I'm looking for.

On Tue, 16 Sep 2008 16:44:03 +0200, "TAB" <an************ *@email.com>
wrote:
>>>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_Enti re4DayWeekRule( 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 StartWeekDayOfY ear =
(int)(new DateTime(date.Y ear, JAN, FIRSTDAYOFJAN)) .DayOfWeek;
int EndWeekDayOfYea r =
(int)(new DateTime(date.Y ear, DEC, LASTDAYOFDEC)). DayOfWeek;

StartWeekDayOfY ear = StartWeekDayOfY ear;
EndWeekDayOfYea r = EndWeekDayOfYea r;
if( StartWeekDayOfY ear == 0)
StartWeekDayOfY ear = 7;
if( EndWeekDayOfYea r == 0)
EndWeekDayOfYea r = 7;

int DaysInFirstWeek = 8 - (StartWeekDayOf Year );
int DaysInLastWeek = 8 - (EndWeekDayOfYe ar );

if (StartWeekDayOf Year == THURSDAY || EndWeekDayOfYea r == THURSDAY)
ThursdayFlag = true;

int FullWeeks = (int) Math.Ceiling((D ayOfYear -
(DaysInFirst Week))/7.0);

int WeekNumber = FullWeeks;

if (DaysInFirstWee k >= THURSDAY)
WeekNumber = WeekNumber +1;

if (WeekNumber 52 && !ThursdayFlag)
WeekNumber = 1;

if (WeekNumber == 0)
WeekNumber = WeekNumber_Enti re4DayWeekRule(
new DateTime(date.Y ear-1, DEC, LASTDAYOFDEC));
return WeekNumber;
}

"TAB" <an************ *@email.comskre v i meddelandet
news:e9***** *********@TK2MS FTNGP02.phx.gbl ...
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(Date Time fromDate)
{
// Get jan 1st of the year
DateTime startOfYear = fromDate.AddDay s(-fromDate.Day +
1).AddMonths (-fromDate.Month + 1);
// Get dec 31st of the year
DateTime endOfYear = startOfYear.Add Years(1).AddDay s(-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[] iso8601Correcti on = { 6, 7, 8, 9, 10, 4, 5 };
int nds = fromDate.Subtra ct(startOfYear) .Days +
iso8601Corre ction[(int)startOfYea r.DayOfWeek];
int wk = nds / 7;
switch (wk)
{
case 0:
// Return weeknumber of dec 31st of the previous year
return WeekNumber(star tOfYear.AddDays (-1));
case 53:
// If dec 31st falls before thursday it is week 01 of
next year
if (endOfYear.DayO fWeek < DayOfWeek.Thurs day)
return 1;
else
return wk;
default: return wk;
}

"Tommy Jakobsen" <to***@holmjako bsen.dkskrev i meddelandet
news:n0***** *************** ************@4a x.com...
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]
<Mo******** ****@hotmail.co mwrote:
>
>>
>>"Clint" wrote:
>>
>>Is there something special about the Danish calendar? i.e. is the
>>answer not
>>always 52? (plus a couple of days)
>>>
>>"Tommy Jakobsen" <to***@holmjako bsen.dkwrote in message
>>news:1m** *************** *************** @4ax.com...
>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.
>>>
>>
>>Sometim es there are 53 weeks in a year.
>>
> int year = 2004;
> DateTime dt = new DateTime(year, 12, 31);
> int week =
>CultureInf o.CurrentCultur e.Calendar.GetW eekOfYear(
> dt,
> CalendarWeekRul e.FirstFourDayW eek,
> DayOfWeek.Monda y);
>>
>>
> // week == 53
Sep 16 '08 #14
TAB


"G.S." <gs******@gmail .comskrev i meddelandet
news:81******** *************** ***********@e53 g2000hsa.google groups.com...
On Sep 16, 10:54 am, Tommy Jakobsen <to...@holmjako bsen.dkwrote:
>That method returns the same as the previous method, doesn't it?

It's still not what I'm looking for.

On Tue, 16 Sep 2008 16:44:03 +0200, "TAB" <anders.bergl.. .@email.com>
wrote:
>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_Enti re4DayWeekRule( 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 StartWeekDayOfY ear =
(int)(new DateTime(date.Y ear, JAN, FIRSTDAYOFJAN)) .DayOfWeek;
int EndWeekDayOfYea r =
(int)(new DateTime(date.Y ear, DEC, LASTDAYOFDEC)). DayOfWeek;
StartWeekDayOfY ear = StartWeekDayOfY ear;
EndWeekDayOfYea r = EndWeekDayOfYea r;
if( StartWeekDayOfY ear == 0)
StartWeekDayOfY ear = 7;
if( EndWeekDayOfYea r == 0)
EndWeekDayOfYea r = 7;
int DaysInFirstWeek = 8 - (StartWeekDayOf Year );
int DaysInLastWeek = 8 - (EndWeekDayOfYe ar );
if (StartWeekDayOf Year == THURSDAY || EndWeekDayOfYea r == THURSDAY)
ThursdayFlag = true;
int FullWeeks = (int) Math.Ceiling((D ayOfYear -
(DaysInFirstWe ek))/7.0);
int WeekNumber = FullWeeks;
if (DaysInFirstWee k >= THURSDAY)
WeekNumber = WeekNumber +1;
if (WeekNumber 52 && !ThursdayFlag)
WeekNumber = 1;
if (WeekNumber == 0)
WeekNumber = WeekNumber_Enti re4DayWeekRule(
new DateTime(date.Y ear-1, DEC, LASTDAYOFDEC));
return WeekNumber;
}
>"TAB" <anders.bergl.. .@email.comskre v i meddelandet
news:e9******* *******@TK2MSFT NGP02.phx.gbl.. .
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(Date Time fromDate)
{
// Get jan 1st of the year
DateTime startOfYear = fromDate.AddDay s(-fromDate.Day +
1).AddMonths (-fromDate.Month + 1);
// Get dec 31st of the year
DateTime endOfYear = startOfYear.Add Years(1).AddDay s(-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[] iso8601Correcti on = { 6, 7, 8, 9, 10, 4, 5 };
int nds = fromDate.Subtra ct(startOfYear) .Days +
iso8601Correct ion[(int)startOfYea r.DayOfWeek];
int wk = nds / 7;
switch (wk)
{
case 0:
// Return weeknumber of dec 31st of the previous year
return WeekNumber(star tOfYear.AddDays (-1));
case 53:
// If dec 31st falls before thursday it is week 01 of
next year
if (endOfYear.DayO fWeek < DayOfWeek.Thurs day)
return 1;
else
return wk;
default: return wk;
}
>"Tommy Jakobsen" <to...@holmjako bsen.dkskrev i meddelandet
news:n0****** *************** ***********@4ax .com...
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]
<MortenWenne. ..@hotmail.comw rote:
>>>>"Clint" wrote:
>>>>Is there something special about the Danish calendar? i.e. is the
answer not
always 52? (plus a couple of days)
>>>>"Tommy Jakobsen" <to...@holmjako bsen.dkwrote in message
>news:1m*** *************** **************@ 4ax.com...
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.Cur rentCulture.Cal endar.GetWeekOf Year(
dt,
CalendarWeek Rule.FirstFourD ayWeek,
DayOfWeek.Mo nday);
>>>// week == 53- Hide quoted text -

- Show quoted text -

I'd echo firts responder's question - what do you consider the first
week?

The example in this article should clarify things a bit:
http://msdn.microsoft.com/en-us/libr...le(VS.95).aspx
It's been a long time since I worked with this so I have to quote
Wikipedia on ISO 8601 regarding week numbers.

The ISO year number deviates from the number of the Gregorian year on, if
applicable, a Friday, Saturday, and Sunday, or a Saturday and Sunday, or
just a Sunday, at the start of the Gregorian year (which are at the end of
the previous ISO year) and a Monday, Tuesday and Wednesday, or a Monday and
Tuesday, or just a Monday, at the end of the Gregorian year (which are in
week 01 of the next ISO year). In the period 4 January-28 December and on
all Thursdays the ISO year number is always equal to the Gregorian year
number.

Mutually equivalent definitions for week 01 are:
the week with the year's first Thursday in it
the week starting with the Monday which is nearest in time to 1 January
the week with the year's first working day in it (if Saturdays, Sundays, and
1 January are not working days)
the week with January 4 in it
the first week with the majority (four or more) of its days in the starting
year
the week starting with the Monday in the period 29 December - 4 January
the week with the Thursday in the period 1 - 7 January
If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week
01. If 1 January is on a Friday, Saturday or Sunday, it is in week 52 or 53
of the previous year.
Note that while most definitions are symmetric with respect to time
reversal, one definition in terms of working days happens to be equivalent.

The last week of the ISO year is the week before week 01; in accordance with
the symmetry of the definition, equivalent definitions are:
the week with the year's last Thursday in it
the week ending with the Sunday which is nearest in time to 31 December
the week with December 28 in it
the last week with the majority (four or more) of its days in the ending
year
the week starting with the Monday in the period 22 - 28 December
the week with the Thursday in the period 25 - 31 December
the week ending with the Sunday in the period 28 December - 3 January
If 31 December is on a Monday, Tuesday, or Wednesday, it is in week 01 of
the next year, otherwise in week 52 or 53.
The following years have 53 weeks:
years starting with Thursday
leap years starting with Wednesday
Sep 16 '08 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1789
by: Orson | last post by:
I have a page that uses the javascript Date() function to retrieve a date from the client computer. The date is then inserted into a note that is saved on a database. The date is in the format Thursay Oct 30 2003 10.47am When I access the page using my computer everything works fine. A colleague in the UK also uses the page and it was fine up until the clocks changed last weekend. Now notes that he saves have the date in the format...
7
15155
by: Shuffs | last post by:
Could someone, anyone please tell me what I need to amend, to get this function to take Sunday as the first day of the week? I amended the Weekday parts to vbSunday (in my code, not the code attached), yet when I ran it for 28/09/2003 (UK date format) it returned Week 39. I would have expected it to return Week 40. However, I'm really stuck and my head is busting over this, so any pointers would be gratefully appreciated. Many thanks...
2
1089
by: | last post by:
Hi!! know someone how to get a week span with week number and year? something like hi: public static DateTime GetWeekSpan(int weekNumber, int year) { DateTime weekStartDate = new DateTime(); DateTime weekEndDate = new DateTime();
3
19931
by: Totto | last post by:
Hi, Is it possible to get number of the week from a DateTime type ? Tnx Totto
3
34831
by: Aussie Rules | last post by:
Hi, How do I extract just the month part of a date. Say for example I wanted to get the value of 6 (as in sixth month) from todays date. Regards
3
2979
by: cassey14 | last post by:
Hi Guys... Im doing a system and it really make my head ache..i hope somebody help me.. I need to have an id like this "ABCD-07-000001"... I dont know how will I work on that thing.. ABCD always a default character..07 is the last two digit of the year and it should be reset everytime a year change..and the last six digit should be incremented by 1 everytime they add a new record but it should be reset when the year change..please help me...
13
2345
by: Gari | last post by:
Dear All, Back to my project database (if you have read my previous thread requests ^^). I have a project database. I will add an edit form that will be used to add new projects. I would like that when adding a new project; a reference is created for this one. This reference will be used as primary key.
2
12163
by: JamesG | last post by:
Hi, How would I convert a week number and year into a date? Thanks!
3
5966
by: Libber39 | last post by:
Hi everyone, Have a query on how to calculate the amount of weeks and days contained in a number in an access query. ie: the difference in days between 2 dates amounts to 17 days. I want to now calculate in the query the amount of weeks and days within the 17 days to show 2 weeks 3 days. I can create the function within excel, (A1 as cell with the days within it) as: =INT(A1/7) & " Weeks, " & MOD(A1,7) & " days" but cant seem to...
7
3202
by: shimul | last post by:
Need to get a formula which will give me rolling year to retrieve data. For Example: when i run query this week, need date >=2008/01/07 and <=2009/01/04 to cover the rolling year (week start "Monday" and week finished "Sunday") (previous week >=2007/12/31 and <=2008/12/28) I have table colume, which call "I_Date" (all date store in this column) and get "week number" and "year" from "form" (pls see attachment). Is there any way I can...
0
9933
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9786
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11125
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10836
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10407
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5794
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4607
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4211
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3230
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.