Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Sir,

I have calculated no of days between two days like that given below:

C#
DateTime startdate = new DateTime();

         startdate =Convert.ToDateTime(DateMontYearToYearMonthDate(stdate.Text));

         DateTime Enddate = new DateTime();

        Enddate = Convert.ToDateTime(DateMontYearToYearMonthDate(endate.Text));

          TimeSpan nod = (Enddate - startdate);

            numdays.Text = nod.TotalDays.ToString();


but i need to calculate no of days between two dates except sunday?

how to do this in c#.if u answer it will more help for me to do this.thanks in advance.
Posted
Updated 17-Jan-13 20:14pm
v2

DateTime startdate = Convert.ToDateTime("01/13/2013");
DateTime enddate = Convert.ToDateTime("01/18/2013");
int count = 0;
while (startdate != enddate)
{
if (startdate.DayOfWeek.CompareTo(DayOfWeek.Sunday) != 0)
{
count++;
startdate = startdate.AddDays(1);
}
else
{
startdate = startdate.AddDays(1);
}
}
MessageBox.Show("no of days"+count);
 
Share this answer
 
There is a solution here: http://stackoverflow.com/questions/1617049/calculate-the-number-of-business-days-between-two-dates[^] which will need a small change to include Saturdays - it assumes a 5 day working week, where you have a six day.

But please, you don't need to convert like that!
C#
startdate =Convert.ToDateTime(DateMontYearToYearMonthDate(stdate.Text));

Don't play with text: look at using DateTime.TryParseExact[^] instead - it lets you specify exactly what the input string should look like, and convert it in a single operation (or report an error if the user entered a bad date).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900