Click here to Skip to main content
12,455,917 members (49,552 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
Hello Sir,

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

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 17-Jan-13 19:26pm
Updated 17-Jan-13 20:14pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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);
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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!
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).
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100