473,779 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

"purkka" <pu****@discuss ions.microsoft. com> wrote in message
news:45******** *************** ***********@mic rosoft.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.Globaliz ation.CultureIn fo myCI = new CultureInfo("en-US");
myCI.Calendar.G etWeekOfYear( DateTime.Now,
System.Globaliz ation.CalendarW eekRule.FirstFo urDayWeek,
System.DayOfWee k.Sunday );

Greetings,
Wessel

-----Original Message-----
From: purkka [mailto:pu****@d iscussions.micr osoft.com]
Posted At: Monday, April 11, 2005 1:02 PM
Posted To: microsoft.publi c.dotnet.framew ork.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.Globaliz ation.CultureIn fo myCI = new CultureInfo("en-US");
myCI.Calendar.G etWeekOfYear( DateTime.Now,
System.Globaliz ation.CalendarW eekRule.FirstFo urDayWeek,
System.DayOfWee k.Sunday );

Greetings,
Wessel

-----Original Message-----
From: purkka [mailto:pu****@d iscussions.micr osoft.com]
Posted At: Monday, April 11, 2005 1:02 PM
Posted To: microsoft.publi c.dotnet.framew ork.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_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 - (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;
}
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(DateIn terval.WeekOfYe ar,Date.Today,v bUseSystemDayOf Week,vbFirstJan 1) & ".")
%>

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****@discuss ions.microsoft. com> wrote in message news:45******** *************** ***********@mic rosoft.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*********@nn oossppaamm-yahoo.com> wrote in message
news:eM******** ******@tk2msftn gp13.phx.gbl...
would this do what you want?:

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

"purkka" <pu****@discuss ions.microsoft. com> wrote in message
news:45******** *************** ***********@mic rosoft.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.ToStr ing()+":"+(((da yOfYear%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...
=============== =======

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

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

"purkka" <pu****@discuss ions.microsoft. com> wrote in message
news:45******** *************** ***********@mic rosoft.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****@d iscussions.micr osoft.com]
Posted At: Monday, April 11, 2005 1:43 PM
Posted To: microsoft.publi c.dotnet.framew ork.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.Globaliz ation.CultureIn fo myCI = new CultureInfo("en-US");
myCI.Calendar.G etWeekOfYear( DateTime.Now,
System.Globaliz ation.CalendarW eekRule.FirstFo urDayWeek,
System.DayOfWee k.Sunday );

Greetings,
Wessel

-----Original Message-----
From: purkka [mailto:pu****@d iscussions.micr osoft.com]
Posted At: Monday, April 11, 2005 1:02 PM
Posted To: microsoft.publi c.dotnet.framew ork.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.t he.sun> wrote in message
news:uv******** ******@TK2MSFTN GP14.phx.gbl...
Try this:

System.Globaliz ation.CultureIn fo myCI = new CultureInfo("en-US");
myCI.Calendar.G etWeekOfYear( DateTime.Now,
System.Globaliz ation.CalendarW eekRule.FirstFo urDayWeek,
System.DayOfWee k.Sunday );

Greetings,
Wessel

-----Original Message-----
From: purkka [mailto:pu****@d iscussions.micr osoft.com]
Posted At: Monday, April 11, 2005 1:02 PM
Posted To: microsoft.publi c.dotnet.framew ork.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****@d iscussions.micr osoft.com]
Posted At: Monday, April 11, 2005 1:43 PM
Posted To: microsoft.publi c.dotnet.framew ork.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.Globaliz ation.CultureIn fo myCI = new CultureInfo("en-US");
myCI.Calendar.G etWeekOfYear( DateTime.Now,
System.Globaliz ation.CalendarW eekRule.FirstFo urDayWeek,
System.DayOfWee k.Sunday );

Greetings,
Wessel

-----Original Message-----
From: purkka [mailto:pu****@d iscussions.micr osoft.com]
Posted At: Monday, April 11, 2005 1:02 PM
Posted To: microsoft.publi c.dotnet.framew ork.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

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

Similar topics

4
5740
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 the week it is . Can anyone help me out with this, regards
7
15149
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
2825
by: Chapai | last post by:
Hi all! I need your help to realize algorithm for stored proc or trigger. tool: SQL TABLE:
3
5934
by: Matt | last post by:
Given a date, how to find the beginning date and ending date of that week please advise!
3
6102
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
1682
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
4231
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 month. So the week would be week 1= (first thursday of the month through the next wed). So for example for July 07 the first thursday is july5th so the first week would be thursday july 5th , friday july 6th, sat july 7th, sun july 8th, mon...
1
2757
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 date field.
5
2839
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, minute etc.. but forgot month. Now i'm adding sort option but i need month but not available during database entry i use the following. $year = date('Y'); $week = date('W'); $day = date('d'); $hour = date('H'); $minute = date('i');.
0
9632
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
10302
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
10071
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
9925
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
8958
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4036
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
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2867
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.