It might be better to replace
if(dt_new.DayOfWeek.ToString()!="Saturday"&&dt_new .DayOfWeek.ToString()!="Sunday")
by
if(dt_new.DayOfWeek != DayOfWeek.Saturday && dt_new.DayOfWeek !=
DayOfWeek.Sunday)
because (a) it should be slightly faster, and (b) it doesn't rely on
locale-specific (if that's the right term) day names.
An even faster method could be to work out the number of complete weeks
(basically divide numberOfDays by 7) and multiply that by 5, then do some
adjustments for the partial weeks at the start and end of the date range -
but unless you're dealing with very long time spans it's probably not worth
the effort!
Chris Jobson
"alan.washington" <alan.washington@aew_nospam.com> wrote in message
news:uz**************@TK2MSFTNGP15.phx.gbl...
DateTime dt_new;
DateTime dt_start, dt_end;
dt_start = Convert.ToDateTime("10/1/2004");
dt_end = Convert.ToDateTime("10/20/2004");
TimeSpan ts = dt_end - dt_start;
int numberOfDays = ts.Days;
for(int i = 0; i<numberOfDays; i++)
{
dt_new = dt_start.AddDays(i);
if(dt_new.DayOfWeek.ToString()!="Saturday"&&dt_new .DayOfWeek.ToString()!="Sunday")
{
listBox1.Items.Add(dt_new.ToString());
}
}
This will just look through every date between the two dates specified and
see whether or not the date is saturday or sunday. If the date is, than
it will not put that date in a listbox. I think this is what you are
looking for.
I have a situation where users will input a start date and an end date,
then return the number of weekdays only. Is this possible with the
DateTime class?
User submitted from AEWNET (http://www.aewnet.com/)