Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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
Edited 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
0 CPallini 510
1 Sergey Alexandrovich Kryukov 453
2 George Jonsson 298
3 Pheonyx 218
4 OriginalGriff 200
0 OriginalGriff 4,813
1 CPallini 3,850
2 Sergey Alexandrovich Kryukov 3,224
3 George Jonsson 2,552
4 Gihan Liyanage 2,186


Advertise | Privacy | Mobile
Web02 | 2.8.140905.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2014
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