473,395 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Get business days

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

David
Sep 19 '07 #1
4 1764
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
"David C" <dl*****@lifetimeinc.comwrote in message
news:eU****************@TK2MSFTNGP06.phx.gbl...
Is there anything in System.DateTime where I can get the number of
weekdays (excluding Saturday and Sunday) between 2 dates? Thanks.

David

Sep 19 '07 #2
A simple loop from the start date to the end date with checking the day and
counting Saturdays and Sundays will do this job very well. You can even
optimize it but I wouldn't bother.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"David C" <dl*****@lifetimeinc.comwrote in message
news:eU****************@TK2MSFTNGP06.phx.gbl...
Is there anything in System.DateTime where I can get the number of
weekdays (excluding Saturday and Sunday) between 2 dates? Thanks.

David

Sep 19 '07 #3
"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:Og****************@TK2MSFTNGP04.phx.gbl...
You can also use an auxiliary calendar table...
That's what I do, plus a bit field to indicate whether the day is a public
holiday or not...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Sep 19 '07 #4
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.aspfa...dar-table.html

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================

"David C" <dl*****@lifetimeinc.comwrote in message news:eU****************@TK2MSFTNGP06.phx.gbl...
Is there anything in System.DateTime where I can get the number of weekdays (excluding Saturday and Sunday) between 2
dates? Thanks.

David


Sep 19 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David Stockwell | last post by:
I''m wondering if the Calendar object has an option to only do calculations based on business days (ie M-F). Additionally does it have a way to get holidays into itself? Currently I'm...
18
by: pb648174 | last post by:
Greeting, below is the complete SQL taken from aspfaq.com (retrieved from this newsgroup I believe) The query takes about two minutes to run. Does anybody have a better set based way (sub-second...
12
by: Anthony Robinson | last post by:
Is anyone aware of a function (system or user defined) that will calculate business days? For instance: I have a column in as table called DATE. I want to be able to add five business days to that...
1
by: igendreau | last post by:
I have users inputting a "Request Date". Upon entering a date, I need Access to populate a second field ("Due Date"). When they enter their Request Date, I want Access to set the default value of...
4
by: CDMAPoster | last post by:
I'm starting to come up with a version of DateAdd that I call BusinessDateAdd that adds the selected number of business days. It's still in preliminary form (needs testing) and interacts with my...
7
by: Sam | last post by:
Hi, I use C# in my ASP.NET projects. Here's what I need to do: I want to add x business days to a given date i.e. add 12 business days to today's date. What is the best, fastest and most...
8
by: =?Utf-8?B?QWw=?= | last post by:
I am working in vb2005. how can I calculate business days (not including holidays and weekends) between 2 dates? thanks Al
1
by: ArchMichael | last post by:
i need help again on calculating business days excluding holidays i have a field called assign date and i need to calculate 7 business days excluding holidays ( already have a table for holiday)...
17
by: trixxnixon | last post by:
i have a form with these fields Priority level: urgent critical standard business days: 1, 3, 15 date submitted: current date due date:
5
FishVal
by: FishVal | last post by:
IMHO, the following is not a how-to-do instruction to solve a particular problem but more a concept-proof stuff demonstrating possibilities of SQL. So, let us say the problem is to calculate...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.