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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
would this do what you want?:

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

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

Greetings,
Wessel

-----Original Message-----
From: purkka [mailto:p[email protected]]
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
 
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 .....
 
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,vbUseSystemDayOfWeek,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...
======================
 
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)?(dayOfYear/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...
======================
 
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...
======================
 
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:p[email protected]]
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 .....
 
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 said:
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:p[email protected]]
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 said:
Try this:

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

Greetings,
Wessel

-----Original Message-----
From: purkka [mailto:p[email protected]]
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
 
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
 
Back
Top