Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

First / Last Day of Week

0 views
Skip to first unread message

Jay Allard

unread,
Dec 9, 2002, 9:24:14 PM12/9/02
to
Hello

In this app, first day of the week can be any week day, which means the last
day of the week is first + 6 or last - 1. Here is how I'm determining it,
but there has to be a better way. Any ideas? This works, but seems
excessive.

The method gets the next LAST DAY OF WEEK for the specified date.
IE: If first day of week is saturday, then the last day is friday. If the
date you give it a wednesday, it needs to add 2.


public static DateTime GetLastDayOfWeek(DateTime dDate, DayOfWeek
FirstDayOfWeek)
{
//there has to be a better way to do this
//figure out last day of the week based on the first.
DayOfWeek LastDay;
switch (FirstDayOfWeek)
{
case DayOfWeek.Friday:
LastDay = Thursday;
break;
case DayOfWeek.Saturday:
LastDay = Friday;
break;
case DayOfWeek.Sunday:
LastDay = Saturday;
break;
case DayOfWeek.Monday:
LastDay = Sunday;
break;
case DayOfWeek.Tuesday:
LastDay = Monday;
break;
case DayOfWeek.Wednesday:
LastDay = Tuesday;
break;
case DayOfWeek.Thursday:
LastDay = Wednesday;
break;
}

//now increment the date until last day is hit
while(dDate.DayOfWeek != LastDay)
dDate = dDate.AddDays(1);
return dDate;


Chris R

unread,
Dec 10, 2002, 12:25:08 AM12/10/02
to
Adding or subtracting enumerated values returns the underlying type,
typically, an int (Int32). Therefore, you can do either of the
following:

public static DateTime GetLastDayOfWeek(DateTime dDate, DayOfWeek
FirstDayOfWeek)
{

DayOfWeek ThisDayOfWeek = dDate.DayOfWeek;
int advance = FirstDayOfWeek - ThisDayOfWeek + 6;
if( advance > 6 )
advance -= 7;
return dDate.AddDays( advance );
}
// OR


public static DateTime GetLastDayOfWeek(DateTime dDate, DayOfWeek
FirstDayOfWeek)
{

DayOfWeek ThisDayOfWeek = dDate.DayOfWeek;
if(ThisDayOfWeek < FirstDayOfWeek)
return dDate.AddDays( FirstDayOfWeek - ThisDayOfWeek - 1 );
else
return dDate.AddDays( FirstDayOfWeek - ThisDayOfWeek + 6 );
}

Chris R.

"Jay Allard" <ne...@allardworks.com> wrote in message
news:3df54f6f$1...@nopics.sjc...

0 new messages