Day Of Week

Just Me

Usenet Archive
Joined
Sep 18, 2000
Messages
258
Reaction score
0
..NET enumerates the day of the week as 0-6 ( Sunday to Saturday ). This seems to remain the same regardless of the culture settings.

Just wondering if this is alterable is some way, and if not how you guys work out for example, next Sunday. I mean sure, I can create a function which will allways return the number of days between today and sunday using for example the day name and a select statement, but it seems too complex.

Any ideas on the simple approach ?
 

Just Me

Usenet Archive
Joined
Sep 18, 2000
Messages
258
Reaction score
0
Dont know if its the best way, but I solved it like this,

public static DateTime thisSunday(DateTime targetDate)

{

int realDay = 1;

if (targetDate.DayOfWeek == 0) realDay = 7; else realDay = (int)targetDate.DayOfWeek;

return targetDate.AddDays(7 - realDay);

}
 

Stan (3)

Usenet Archive
Joined
Mar 16, 2008
Messages
71
Reaction score
0
Hi

The problem is not that the enumeration is 0 - 6. The complication arises because you apparently require a logically different result for Sunday than for other days of the week.

According to your solution if targetDate is Monday to Saturday then you want to return the date for the *next* Sunday whereas if targetDate is Sunday then you want to return the date unaltered i.e.
*this* Sunday.

For example if the code were written thus:

DateTime NextSunday(DateTime targetDate)
{
int dow = (int)targetDate.DayOfWeek;
return targetDate.AddDays(7 - dow);
}

the result would be the same as your solution except when targetDate is Sunday, in which case you get next Sunday i.e. a week later.
 

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

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,243
Members
248,133
Latest member
DujonT

Latest Threads

Top