12,455,917 members (49,552 online)
Rate this:
See more:
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:

## 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++;
}
else
{
}
}
MessageBox.Show("no of days"+count);
Rate this:

## 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).

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

Top Experts
Last 24hrsThis month
 Maciej Los 318 OriginalGriff 305 phil.o 194 Dave Kreskowiak 167 ppolymorphe 157
 OriginalGriff 6,678 ppolymorphe 4,035 Maciej Los 3,798 Karthik Bangalore 3,665 Richard Deeming 2,835

Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 18 Jan 2013