By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,364 Members | 1,278 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,364 IT Pros & Developers. It's quick & easy.

How to find out a week of the day in C#?

P: n/a
Hi

I did not find any solution from Internet how to do it. There is a lot of
samples how to retrieve day of the week, but not number of the week.

With rgds

MP
Nov 19 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
would this do what you want?:

Dim theDate as DateTime = DateTime.Now
Dim WeekOfYear as integer = Math.Ceiling(theDate.DayOfYear / 7)

"purkka" <pu****@discussions.microsoft.com> wrote in message
news:45**********************************@microsof t.com...
Hi

I did not find any solution from Internet how to do it. There is a lot of
samples how to retrieve day of the week, but not number of the week.

With rgds

MP

Nov 19 '05 #2

P: n/a
Try this:

System.Globalization.CultureInfo myCI = new CultureInfo("en-US");
myCI.Calendar.GetWeekOfYear( DateTime.Now,
System.Globalization.CalendarWeekRule.FirstFourDay Week,
System.DayOfWeek.Sunday );

Greetings,
Wessel

-----Original Message-----
From: purkka [mailto:pu****@discussions.microsoft.com]
Posted At: Monday, April 11, 2005 1:02 PM
Posted To: microsoft.public.dotnet.framework.aspnet
Conversation: How to find out a week of the day in C#?
Subject: How to find out a week of the day in C#?

Hi

I did not find any solution from Internet how to do it. There is a lot
of
samples how to retrieve day of the week, but not number of the week.

With rgds

MP

Nov 19 '05 #3

P: n/a
Hoops

Sorry. I did not mentioned, that my ASP.NET application fetch datevalues
(1/2/2005, 11/2/2005...) from Access db. Values are shown as a list in a
datagrid which should also provide week numbers like this:

Week Date Name .....
-------------------------------
15 12.4.2005 John Smith .....

With rgds
MP

"Wessel Troost" wrote:
Try this:

System.Globalization.CultureInfo myCI = new CultureInfo("en-US");
myCI.Calendar.GetWeekOfYear( DateTime.Now,
System.Globalization.CalendarWeekRule.FirstFourDay Week,
System.DayOfWeek.Sunday );

Greetings,
Wessel

-----Original Message-----
From: purkka [mailto:pu****@discussions.microsoft.com]
Posted At: Monday, April 11, 2005 1:02 PM
Posted To: microsoft.public.dotnet.framework.aspnet
Conversation: How to find out a week of the day in C#?
Subject: How to find out a week of the day in C#?

Hi

I did not find any solution from Internet how to do it. There is a lot
of
samples how to retrieve day of the week, but not number of the week.

With rgds

MP

Nov 19 '05 #4

P: n/a
Simen Sandelien wrote this nifty code to do just that :

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;
}
If you want to read the comments to the code,
see http://konsulent.sandelien.no/VB_help/Week/

I edited them out so the post would be shorter.
If you could use Visual Basic, you could simply do :

<%
Response.Write("This is this year's week number " & DatePart(DateInterval.WeekOfYear,Date.Today,vbUseS ystemDayOfWeek,vbFirstJan1) & ".")
%>

Maybe you could simply compile an assembly in VB.NET
which sets the week number, and call it from C#.

That seems a lot simpler than using the convoluted C# function quoted above.

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"purkka" <pu****@discussions.microsoft.com> wrote in message news:45**********************************@microsof t.com...
Hi

I did not find any solution from Internet how to do it. There is a lot of
samples how to retrieve day of the week, but not number of the week.

With rgds

MP

Nov 19 '05 #5

P: n/a
That code will produce output errors,
as the result is not compatible with ISO 8601.

See http://konsulent.sandelien.no/VB_help/Week/


Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Joseph Byrns" <jo*********@nnoossppaamm-yahoo.com> wrote in message
news:eM**************@tk2msftngp13.phx.gbl...
would this do what you want?:

Dim theDate as DateTime = DateTime.Now
Dim WeekOfYear as integer = Math.Ceiling(theDate.DayOfYear / 7)

"purkka" <pu****@discussions.microsoft.com> wrote in message
news:45**********************************@microsof t.com...
Hi

I did not find any solution from Internet how to do it. There is a lot of
samples how to retrieve day of the week, but not number of the week.

With rgds

MP


Nov 19 '05 #6

P: n/a
I should have added that this similar function
would be incorrect for the same reason :

System.DateTime dt = System.DateTime.Now;
int dayOfYear = dt.DayOfYear;
Label1.Text =
dayOfYear.ToString()+":"+(((dayOfYear%7)==0)?(dayO fYear/7):(dayOfYear/7)+1).ToString();

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Joseph Byrns" <jo*********@nnoossppaamm-yahoo.com> wrote in message
news:eM**************@tk2msftngp13.phx.gbl...
would this do what you want?:

Dim theDate as DateTime = DateTime.Now
Dim WeekOfYear as integer = Math.Ceiling(theDate.DayOfYear / 7)

"purkka" <pu****@discussions.microsoft.com> wrote in message
news:45**********************************@microsof t.com...
Hi

I did not find any solution from Internet how to do it. There is a lot of
samples how to retrieve day of the week, but not number of the week.

With rgds

MP


Nov 19 '05 #7

P: n/a
If you're fetching the dates from SQL, you can try something like:

SELECT DATEPART('wk', YourDateField) As Week FROM YourTable

This works on SQL Server, not sure if it works with an Access backend.
This method always returns US weeknumbers.

Regards,
Wessel

-----Original Message-----
From: purkka [mailto:pu****@discussions.microsoft.com]
Posted At: Monday, April 11, 2005 1:43 PM
Posted To: microsoft.public.dotnet.framework.aspnet
Conversation: How to find out a week of the day in C#?
Subject: Re: How to find out a week of the day in C#?

Hoops

Sorry. I did not mentioned, that my ASP.NET application fetch datevalues

(1/2/2005, 11/2/2005...) from Access db. Values are shown as a list in
a
datagrid which should also provide week numbers like this:

Week Date Name .....
-------------------------------
15 12.4.2005 John Smith .....

With rgds
MP

"Wessel Troost" wrote:
Try this:

System.Globalization.CultureInfo myCI = new CultureInfo("en-US");
myCI.Calendar.GetWeekOfYear( DateTime.Now,
System.Globalization.CalendarWeekRule.FirstFourDay Week,
System.DayOfWeek.Sunday );

Greetings,
Wessel

-----Original Message-----
From: purkka [mailto:pu****@discussions.microsoft.com]
Posted At: Monday, April 11, 2005 1:02 PM
Posted To: microsoft.public.dotnet.framework.aspnet
Conversation: How to find out a week of the day in C#?
Subject: How to find out a week of the day in C#?

Hi

I did not find any solution from Internet how to do it. There is a lot
of
samples how to retrieve day of the week, but not number of the week.

With rgds

MP


Nov 19 '05 #8

P: n/a
Simen Sandelien says ( Simen says? ) that will
produce results incompatible with ISO 8601

http://konsulent.sandelien.no/VB_help/Week/

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."

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Wessel Troost" <no*****@like.the.sun> wrote in message
news:uv**************@TK2MSFTNGP14.phx.gbl...
Try this:

System.Globalization.CultureInfo myCI = new CultureInfo("en-US");
myCI.Calendar.GetWeekOfYear( DateTime.Now,
System.Globalization.CalendarWeekRule.FirstFourDay Week,
System.DayOfWeek.Sunday );

Greetings,
Wessel

-----Original Message-----
From: purkka [mailto:pu****@discussions.microsoft.com]
Posted At: Monday, April 11, 2005 1:02 PM
Posted To: microsoft.public.dotnet.framework.aspnet
Conversation: How to find out a week of the day in C#?
Subject: How to find out a week of the day in C#?

Hi

I did not find any solution from Internet how to do it. There is a lot
of
samples how to retrieve day of the week, but not number of the week.

With rgds

MP

Nov 19 '05 #9

P: n/a
Thanks for your reply. That was not help me out, but Juan T. Llibre's one
did. Your tip is useful as well.

Rgds MP

"Wessel Troost" wrote:
If you're fetching the dates from SQL, you can try something like:

SELECT DATEPART('wk', YourDateField) As Week FROM YourTable

This works on SQL Server, not sure if it works with an Access backend.
This method always returns US weeknumbers.

Regards,
Wessel

-----Original Message-----
From: purkka [mailto:pu****@discussions.microsoft.com]
Posted At: Monday, April 11, 2005 1:43 PM
Posted To: microsoft.public.dotnet.framework.aspnet
Conversation: How to find out a week of the day in C#?
Subject: Re: How to find out a week of the day in C#?

Hoops

Sorry. I did not mentioned, that my ASP.NET application fetch datevalues

(1/2/2005, 11/2/2005...) from Access db. Values are shown as a list in
a
datagrid which should also provide week numbers like this:

Week Date Name .....
-------------------------------
15 12.4.2005 John Smith .....

With rgds
MP

"Wessel Troost" wrote:
Try this:

System.Globalization.CultureInfo myCI = new CultureInfo("en-US");
myCI.Calendar.GetWeekOfYear( DateTime.Now,
System.Globalization.CalendarWeekRule.FirstFourDay Week,
System.DayOfWeek.Sunday );

Greetings,
Wessel

-----Original Message-----
From: purkka [mailto:pu****@discussions.microsoft.com]
Posted At: Monday, April 11, 2005 1:02 PM
Posted To: microsoft.public.dotnet.framework.aspnet
Conversation: How to find out a week of the day in C#?
Subject: How to find out a week of the day in C#?

Hi

I did not find any solution from Internet how to do it. There is a lot
of
samples how to retrieve day of the week, but not number of the week.

With rgds

MP


Nov 19 '05 #10

P: n/a
Thanks a lot!!! That was exactly i was looking for. I had similar procedure
done by myself, but I imagined that C# has easier way to do it. Anyway, your
code is better than mine. Thanks once again.

Kindest rgds MP

"Juan T. Llibre" wrote:
Simen Sandelien wrote this nifty code to do just that :

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;
}
If you want to read the comments to the code,
see http://konsulent.sandelien.no/VB_help/Week/

I edited them out so the post would be shorter.
If you could use Visual Basic, you could simply do :

<%
Response.Write("This is this year's week number " & DatePart(DateInterval.WeekOfYear,Date.Today,vbUseS ystemDayOfWeek,vbFirstJan1) & ".")
%>

Maybe you could simply compile an assembly in VB.NET
which sets the week number, and call it from C#.

That seems a lot simpler than using the convoluted C# function quoted above.

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"purkka" <pu****@discussions.microsoft.com> wrote in message news:45**********************************@microsof t.com...
Hi

I did not find any solution from Internet how to do it. There is a lot of
samples how to retrieve day of the week, but not number of the week.

With rgds

MP

Nov 19 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.