473,387 Members | 1,771 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,387 software developers and data experts.

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 3200
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
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
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(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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
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...
18
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. ...
3
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 *...
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...
2
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...
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...
4
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,...
1
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...
0
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.