Get business days

D

David C

Is there anything in System.DateTime where I can get the number of weekdays
(excluding Saturday and Sunday) between 2 dates? Thanks.

David
 
J

John Timney \(MVP\)

Something like this should do - initially came from code example at
aspalliance

public int GetWeekdays(DateTime startTime, DateTime endTime)
{
TimeSpan timeSp = endTime - startTime;
int iCounter = 0;
for (int i=0; i<timeSp.Days; i++)
{
DateTime dateTm = startTime.AddDays(i);
if (IsWeekDay(dateTm))
{
iCounter++;
}
}
return iCounter;
}


public bool IsWeekDay(DateTime dt)
{
if ( (dt.DayOfWeek == DayOfWeek.Saturday) || (dt.DayOfWeek ==
DayOfWeek.Sunday) )
{
return false;
}
else
{
return true;
}
}

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog
 
J

Juan T. Llibre

Maybe this will help...if you're using SQL Server.

set datefirst 1
declare @sdate datetime
declare @edate datetime

select @sdate = '20070516' --for example, start date May, 16th
select @edate='20070531' --end date May, 31st

select datediff(day, @sdate, @edate)+1-(
select (case datepart(dw, @sdate)
when 7 then (datepart(ww, @edate)-datepart(ww, @sdate))*2-1
else (datepart(ww, @edate)-datepart(ww, @sdate))*2
end)+
(case datepart(dw, @edate)
when 6 then 1
when 7 then 2
else 0
end)
)

You can also use an auxiliary calendar table...

See :

http://sqlserver2000.databases.aspf...nsider-using-an-auxiliary-calendar-table.html
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top