473,546 Members | 2,644 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

count days including weekend

I have a routine that's calculating business days but its not counting the
weekend days that are between the start date and end date. If my start date
is 9/26/08 and my end date is 10/01/08, I should see 4 business days and 2
weekend days.

How can I get that result? I'm getting 4 business days but its not counting
the weekend days?
Sep 26 '08 #1
7 3212
On Sep 26, 8:31*am, "Mike" <whyyoulookinga ...@gmail.comwr ote:
I have a routine that's calculating business days but its not counting the
weekend days that are between the start date and end date. If my start date
is 9/26/08 and my end date is 10/01/08, I should see 4 business days and 2
weekend days.

How can I get that result? I'm getting 4 business days but its not counting
the weekend days?
here is a simple code I wrote

static void Main(string[] args)
{
DateTime st = DateTime.Parse( "01/01/2008");
DateTime et = DateTime.Parse( "01/10/2008");
DateTime dt = st;
int weekend =0;
int weekdays = 0;
while (dt < et)
{
Console.WriteLi ne("Day of the week {0}",
dt.DayOfWeek);
int temp = ((dt.DayOfWeek == DayOfWeek.Satur day) ||
(dt.DayOfWeek == DayOfWeek.Sunda y)) ? weekend++ : weekdays++;
dt = dt.AddDays(1);
}
Console.Read();
}
Sep 26 '08 #2
I tried your code snippet and its not working, If I enter in "09/26/2008"
and "10/01/2008" it returns 1 weekend day, it should be 2.

"CSharper" <cs******@gmx.c omwrote in message
news:36******** *************** ***********@f63 g2000hsf.google groups.com...
On Sep 26, 8:31 am, "Mike" <whyyoulookinga ...@gmail.comwr ote:
I have a routine that's calculating business days but its not counting the
weekend days that are between the start date and end date. If my start
date
is 9/26/08 and my end date is 10/01/08, I should see 4 business days and 2
weekend days.

How can I get that result? I'm getting 4 business days but its not
counting
the weekend days?
here is a simple code I wrote

static void Main(string[] args)
{
DateTime st = DateTime.Parse( "01/01/2008");
DateTime et = DateTime.Parse( "01/10/2008");
DateTime dt = st;
int weekend =0;
int weekdays = 0;
while (dt < et)
{
Console.WriteLi ne("Day of the week {0}",
dt.DayOfWeek);
int temp = ((dt.DayOfWeek == DayOfWeek.Satur day) ||
(dt.DayOfWeek == DayOfWeek.Sunda y)) ? weekend++ : weekdays++;
dt = dt.AddDays(1);
}
Console.Read();
}
Sep 26 '08 #3
Can you post your code?

"Mike" wrote:
I have a routine that's calculating business days but its not counting the
weekend days that are between the start date and end date. If my start date
is 9/26/08 and my end date is 10/01/08, I should see 4 business days and 2
weekend days.

How can I get that result? I'm getting 4 business days but its not counting
the weekend days?
Sep 26 '08 #4
I'm trying this which I found online some time back:
public void AddWeekdays(Dat eTime start, int days)
{
try
{
if (start.DayOfWee k == DayOfWeek.Satur day || start.DayOfWeek ==
DayOfWeek.Sunda y)
{

}
else
{
int remainder = days % 5;
int weekend = (5 / days) *2;

DateTime end = start.AddDays(r emainder);
if (end.DayOfWeek == DayOfWeek.Satur day)
{
end = end.AddDays(2);
}
else if (end.DayOfWeek < start.DayOfWeek )
{
end = end.AddDays(2);
}
txtWDays.Value = weekend.ToStrin g();
txtCDates.Text = end.AddDays(day s + weekend -
remainder).ToSh ortDateString() ;
}

}
catch (Exception ex)
{
}
}

"Family Tree Mike" <Fa************ @discussions.mi crosoft.comwrot e in
message news:43******** *************** ***********@mic rosoft.com...
Can you post your code?

"Mike" wrote:
>I have a routine that's calculating business days but its not counting
the
weekend days that are between the start date and end date. If my start
date
is 9/26/08 and my end date is 10/01/08, I should see 4 business days and
2
weekend days.

How can I get that result? I'm getting 4 business days but its not
counting
the weekend days?

Sep 26 '08 #5
On Sep 26, 9:26*am, Family Tree Mike
<FamilyTreeM... @discussions.mi crosoft.comwrot e:
Can you post your code?

"Mike" wrote:
I have a routine that's calculating business days but its not counting the
weekend days that are between the start date and end date. If my start date
is 9/26/08 and my end date is 10/01/08, I should see 4 business days and 2
weekend days.
How can I get that result? I'm getting 4 business days but its not counting
the weekend days?
When I ran the same code changing the date to what you specified I got
3 weekdays and 2 weekends. This is what I changed

static void Main(string[] args)
{
DateTime st = DateTime.Parse( "09/26/2008");
DateTime et = DateTime.Parse( "10/01/2008");
DateTime dt = st;
int weekend =0;
int weekdays = 0;
while (dt < et)
{
Console.WriteLi ne("Day of the week {0}",
dt.DayOfWeek);
int temp = ((dt.DayOfWeek == DayOfWeek.Satur day) ||
(dt.DayOfWeek == DayOfWeek.Sunda y)) ? weekend++ : weekdays++;
dt = dt.AddDays(1);
}
Console.WriteLi ne("Weekdays {0} and Weekends {1}",
weekdays, weekend);
Console.Read();
}
Sep 26 '08 #6
On Sep 26, 9:32*am, "Mike" <whyyoulookinga ...@gmail.comwr ote:
I'm trying this which I found online some time back:
public void AddWeekdays(Dat eTime start, int days)
* * {
* * * * try
* * * * {
* * * * * * if (start.DayOfWee k == DayOfWeek.Satur day || start.DayOfWeek ==
DayOfWeek.Sunda y)
* * * * * * {

* * * * * *}
* * * * * *else
* * * * * * {
* * * * * * * * int remainder = days % 5;
* * * * * * * * int weekend = (5 / days) *2;

* * * * * * * * DateTime end = start.AddDays(r emainder);
* * * * * * * * if (end.DayOfWeek == DayOfWeek.Satur day)
* * * * * * * * {
* * * * * * * * * * end = end.AddDays(2);
* * * * * * * * }
* * * * * * * * else if (end.DayOfWeek < start.DayOfWeek )
* * * * * * * * {
* * * * * * * * * * end = end.AddDays(2);
* * * * * * * * }
* * * * * * * * txtWDays.Value = weekend.ToStrin g();
* * * * * * * * txtCDates.Text = end.AddDays(day s + weekend -
remainder).ToSh ortDateString() ;
* * * * * * }

* * * * }
* * * * catch (Exception ex)
* * * * {
* * * * }
* * }

"Family Tree Mike" <FamilyTreeM... @discussions.mi crosoft.comwrot e in
messagenews:43* *************** *************** ***@microsoft.c om...
Can you post your code?
"Mike" wrote:
I have a routine that's calculating business days but its not counting
the
weekend days that are between the start date and end date. If my start
date
is 9/26/08 and my end date is 10/01/08, I should see 4 business days and
2
weekend days.
How can I get that result? I'm getting 4 business days but its not
counting
the weekend days?
If you still want to use your code logic, you need change the code
like the following

public void AddWeekdays(Dat eTime start, int days)
{
try
{
if (start.DayOfWee k == DayOfWeek.Satur day ||
start.DayOfWeek ==
DayOfWeek.Sunda y)
{
}
else
{
int remainder = days % 5;
int weekend = ( days / 5) *2; // change here
DateTime end = start.AddDays(r emainder);
if (end.DayOfWeek == DayOfWeek.Satur day)
{
end = end.AddDays(2);
}
else if (end.DayOfWeek < start.DayOfWeek )
{
end = end.AddDays(2);
}
txtWDays.Value = weekend.ToStrin g();
txtCDates.Text = end.AddDays(day s - weekend -
remainder).ToSh ortDateString() ; //change here
}
}
catch (Exception ex)
{
}
}

Try this code and let me know if it works.
Sep 26 '08 #7
On Sep 26, 10:23 am, "Mike" <whyyoulookinga ...@gmail.comwr ote:
I tried your code snippet and its not working, If I enter in "09/26/2008"
and "10/01/2008" it returns 1 weekend day, it should be 2.

"CSharper" <cshar...@gmx.c omwrote in message

news:36******** *************** ***********@f63 g2000hsf.google groups.com...
On Sep 26, 8:31 am, "Mike" <whyyoulookinga ...@gmail.comwr ote:
I have a routine that's calculating business days but its not counting the
weekend days that are between the start date and end date. If my start
date
is 9/26/08 and my end date is 10/01/08, I should see 4 business days and 2
weekend days.
How can I get that result? I'm getting 4 business days but its not
counting
the weekend days?

here is a simple code I wrote

static void Main(string[] args)
{
DateTime st = DateTime.Parse( "01/01/2008");
DateTime et = DateTime.Parse( "01/10/2008");
DateTime dt = st;
int weekend =0;
int weekdays = 0;
while (dt < et)
{
Console.WriteLi ne("Day of the week {0}",
dt.DayOfWeek);
int temp = ((dt.DayOfWeek == DayOfWeek.Satur day) ||
(dt.DayOfWeek == DayOfWeek.Sunda y)) ? weekend++ : weekdays++;
dt = dt.AddDays(1);
}
Console.Read();
}
I think that you would have to iterate.
I have a req. to count the business days in a month.This is the code
I'm using:
static int BusinessDaysInM onth(DateTime dt) {
DateTime startDate = dt.AddDays(1 - dt.Day); //make
sure we are on day 1 of the month
int days = 1;
while (startDate.Mont h == dt.Month) {
if ((startDate.Day OfWeek != DayOfWeek.Satur day) &&
(startDate.DayO fWeek != DayOfWeek.Sunda y))
days++;
startDate = startDate.AddDa ys(1);
}
return days;
}
Sep 26 '08 #8

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

Similar topics

5
14874
by: BlackFireNova | last post by:
I need to write a report in which one part shows a count of how many total records fall within the working days (Monday - Friday) inside of a (prompted) given date range, in a particular geographical region. I have written a query which prompts the user for the start and end dates. It also filters for entries which pertain to the particular...
18
5906
by: jimfortune | last post by:
I have an A97 module called modWorkdayFunctions in: http://www.oakland.edu/~fortune/WorkdayFunctions.zip It allows the counting of workdays taking into consideration up to 11 U.S. holidays. More holidays can be added easily. Also, there is a boolean flag for including or excluding Saturdays or Sundays as part of the work week. The only...
3
2851
by: chrisperkins99 | last post by:
It seems to me that str.count is awfully slow. Is there some reason for this? Evidence: ######## str.count time test ######## import string import time import array s = string.printable * int(1e5) # 10**7 character string
1
3721
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 Due Date = Request Date + 9 Business Days (Holidays don't matter. Just want Saturday and Sunday taken out). Any thoughts?
2
3131
by: MLH | last post by:
With a table of holidays and A97's date fn's - how best to count weekends and holidays between two dates? My holiday table has 4 fields. I will be adding records to it each year as info becomes known. What approach would you advise to tally up the number of holidays, saturdays and sundays between any 2 dates in the range of my records in...
7
25974
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 efficient way for me to do this? -- Thanks, Sam
4
2838
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi, I have a web application that I need to add 3 days to the Now day, but need to make sure that I skip weekends and holidays. For example if Now is friday, 3 days + now should be tuesday, counting the current day as the first day. Now would never occure on a weekend or holiday. Anyhow just wondering if anyone has any ideas? Thanks. --...
1
1566
by: geraldjr30 | last post by:
hi, i have the following to calculate working days between 2 dates, but i am not getting the right amount. i should be getting 11 instead of 12, because feb 16 2009 was a holiday. can someone please advise? here is my code: <?php //The function returns the no. of business days between two dates and it skips the holidays function...
0
1886
by: geraldjr30 | last post by:
hi, i have the following: <?php echo"test"; //The function returns the no. of business days between two dates and it skips the holidays function getWorkingDaysTOT($startDate2,$endDate2,$HOLS){ //The total number of days between the two dates. We compute the no. of seconds and divide it to 60*60*24
0
7694
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7792
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6026
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3491
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.