473,320 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 #1
10 2725
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: chennakeshava_ramesh | last post by:
hi, I have a problem, I am not able to find out which day of the week it is using the calendar class. I am using set() function to set the date and want to find out which day i.e mon,tue etc of...
7
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...
2
by: Chapai | last post by:
Hi all! I need your help to realize algorithm for stored proc or trigger. tool: SQL TABLE:
3
by: Matt | last post by:
Given a date, how to find the beginning date and ending date of that week please advise!
3
by: Steph. | last post by:
Hi, When I use the "Calendar.GetWeekOfYear" function (with "fr-BE" as CultureInfo and Monday as the first day of week) I get : Friday 31/12/2004 : week = 53
3
by: Adam | last post by:
Hello, How to find a last day of the next week (or in next two weeks) using VB.NET? for example: 01-Feb-2005 = 13-Feb-2005 or 09-May-2005 = 22-May-2005
6
by: =?Utf-8?B?UGF1bA==?= | last post by:
HI I have a stored procedure that returns data with a date field in the form of a DateTime type. I need to place data in variables based on days of the week starting with the first thursday of the...
1
by: Simon | last post by:
Dear reader, Is there a function or VBA code available to find the week number out of a date field. You can find the year with Year(date field) but now I need the week number out of a...
5
by: cssExp | last post by:
the problem is, i have a dynamic database driven site, each data is entered with year, week etc.. 2 months ago assuming I'll create sort option in future i put everything, i.e year, week, hour,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.