Day Of Week

  • Thread starter Thread starter Just Me
  • Start date Start date
J

Just Me

..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 ?
 
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);

}
 
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.
 
Back
Top