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

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
 
J

Joseph Byrns

would this do what you want?:

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

Wessel Troost

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
 
G

Guest

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

Juan T. Llibre

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

Juan T. Llibre

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();
 
J

Juan T. Llibre

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

Wessel Troost

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

Guest

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top