By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,680 Members | 1,508 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,680 IT Pros & Developers. It's quick & easy.

count days including weekend

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
On Sep 26, 8:31*am, "Mike" <whyyoulookinga...@gmail.comwrote:
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.WriteLine("Day of the week {0}",
dt.DayOfWeek);
int temp = ((dt.DayOfWeek == DayOfWeek.Saturday) ||
(dt.DayOfWeek == DayOfWeek.Sunday)) ? weekend++ : weekdays++;
dt = dt.AddDays(1);
}
Console.Read();
}
Sep 26 '08 #2

P: n/a
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.comwrote in message
news:36**********************************@f63g2000 hsf.googlegroups.com...
On Sep 26, 8:31 am, "Mike" <whyyoulookinga...@gmail.comwrote:
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.WriteLine("Day of the week {0}",
dt.DayOfWeek);
int temp = ((dt.DayOfWeek == DayOfWeek.Saturday) ||
(dt.DayOfWeek == DayOfWeek.Sunday)) ? weekend++ : weekdays++;
dt = dt.AddDays(1);
}
Console.Read();
}
Sep 26 '08 #3

P: n/a
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

P: n/a
I'm trying this which I found online some time back:
public void AddWeekdays(DateTime start, int days)
{
try
{
if (start.DayOfWeek == DayOfWeek.Saturday || start.DayOfWeek ==
DayOfWeek.Sunday)
{

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

DateTime end = start.AddDays(remainder);
if (end.DayOfWeek == DayOfWeek.Saturday)
{
end = end.AddDays(2);
}
else if (end.DayOfWeek < start.DayOfWeek)
{
end = end.AddDays(2);
}
txtWDays.Value = weekend.ToString();
txtCDates.Text = end.AddDays(days + weekend -
remainder).ToShortDateString();
}

}
catch (Exception ex)
{
}
}

"Family Tree Mike" <Fa************@discussions.microsoft.comwrote in
message news:43**********************************@microsof t.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

P: n/a
On Sep 26, 9:26*am, Family Tree Mike
<FamilyTreeM...@discussions.microsoft.comwrote:
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.WriteLine("Day of the week {0}",
dt.DayOfWeek);
int temp = ((dt.DayOfWeek == DayOfWeek.Saturday) ||
(dt.DayOfWeek == DayOfWeek.Sunday)) ? weekend++ : weekdays++;
dt = dt.AddDays(1);
}
Console.WriteLine("Weekdays {0} and Weekends {1}",
weekdays, weekend);
Console.Read();
}
Sep 26 '08 #6

P: n/a
On Sep 26, 9:32*am, "Mike" <whyyoulookinga...@gmail.comwrote:
I'm trying this which I found online some time back:
public void AddWeekdays(DateTime start, int days)
* * {
* * * * try
* * * * {
* * * * * * if (start.DayOfWeek == DayOfWeek.Saturday || start.DayOfWeek ==
DayOfWeek.Sunday)
* * * * * * {

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

* * * * * * * * DateTime end = start.AddDays(remainder);
* * * * * * * * if (end.DayOfWeek == DayOfWeek.Saturday)
* * * * * * * * {
* * * * * * * * * * end = end.AddDays(2);
* * * * * * * * }
* * * * * * * * else if (end.DayOfWeek < start.DayOfWeek)
* * * * * * * * {
* * * * * * * * * * end = end.AddDays(2);
* * * * * * * * }
* * * * * * * * txtWDays.Value = weekend.ToString();
* * * * * * * * txtCDates.Text = end.AddDays(days + weekend -
remainder).ToShortDateString();
* * * * * * }

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

"Family Tree Mike" <FamilyTreeM...@discussions.microsoft.comwrote in
messagenews:43**********************************@m icrosoft.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?
If you still want to use your code logic, you need change the code
like the following

public void AddWeekdays(DateTime start, int days)
{
try
{
if (start.DayOfWeek == DayOfWeek.Saturday ||
start.DayOfWeek ==
DayOfWeek.Sunday)
{
}
else
{
int remainder = days % 5;
int weekend = ( days / 5) *2; // change here
DateTime end = start.AddDays(remainder);
if (end.DayOfWeek == DayOfWeek.Saturday)
{
end = end.AddDays(2);
}
else if (end.DayOfWeek < start.DayOfWeek)
{
end = end.AddDays(2);
}
txtWDays.Value = weekend.ToString();
txtCDates.Text = end.AddDays(days - weekend -
remainder).ToShortDateString(); //change here
}
}
catch (Exception ex)
{
}
}

Try this code and let me know if it works.
Sep 26 '08 #7

P: n/a
On Sep 26, 10:23 am, "Mike" <whyyoulookinga...@gmail.comwrote:
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.comwrote in message

news:36**********************************@f63g2000 hsf.googlegroups.com...
On Sep 26, 8:31 am, "Mike" <whyyoulookinga...@gmail.comwrote:
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.WriteLine("Day of the week {0}",
dt.DayOfWeek);
int temp = ((dt.DayOfWeek == DayOfWeek.Saturday) ||
(dt.DayOfWeek == DayOfWeek.Sunday)) ? 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 BusinessDaysInMonth(DateTime dt) {
DateTime startDate = dt.AddDays(1 - dt.Day); //make
sure we are on day 1 of the month
int days = 1;
while (startDate.Month == dt.Month) {
if ((startDate.DayOfWeek != DayOfWeek.Saturday) &&
(startDate.DayOfWeek != DayOfWeek.Sunday))
days++;
startDate = startDate.AddDays(1);
}
return days;
}
Sep 26 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.