how to caluclate the days between the two selected dates [Resolved]

Posted by Siddu1281 under ASP.NET AJAX on 9/7/2012 | Points: 10 | Views : 6052 | Status : [Member] | Replies : 3
hello friends,

Iam developing an leave management application. i want to find number of days a member applied the leaves. iam getting error in finding the days between the two selected dates in ajax calender tool kit.

my error is
Compiler Error Message: CS0029: Cannot implicitly convert type 'System.TimeSpan' to 'System.DateTime'

my code is

tbStartdate_CalendarExtender.StartDate = DateTime.Today;
tbEndDate_CalendarExtender.StartDate = DateTime.Today.AddDays(1);

DateTime StartDate1 = DateTime.ParseExact(tbStartdate.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
DateTime EndDate1 = DateTime.ParseExact(tbEnddate.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);

DateTime leavesRemaining1 = EndDate1.Subtract(StartDate1);





Responses

Posted by: Nithadeepak on: 9/7/2012 [Member] Bronze | Points: 50

Up
0
Down

Resolved
EndDate1.Subtract(StartDate1) returns a TimeSpan type.

Try this

System.TimeSpan diffResult = EndDate1.Subtract(StartDate1);
int noOfDays=Convert.ToInt32(diffResult.Days);


Nitha Deepak

Siddu1281, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Siddu1281 on: 9/7/2012 [Member] Starter | Points: 25

Up
0
Down

thank you NithaDeepak.the code u given helped me


Siddu1281, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijay.K on: 9/7/2012 [Member] Starter | Points: 25

Up
0
Down
private void btn_total_Click(object sender, EventArgs e)
{
int Totaldays = duration(textBox1.Text, textBox2.Text);
label1.Text = Convert.ToString(Totaldays);
}

public int duration(string startdate, string enddate)
{

DateTime dt1 = DateTime.Parse(Convert.ToDateTime(textBox1.Text).ToString("dd/MM/yyyy"));
DateTime dt2 = DateTime.Parse(Convert.ToDateTime(textBox2.Text).ToString("dd/MM/yyyy"));
TimeSpan ts = dt2.Subtract(dt1);
int days = ts.Days;
return days;
}

vijay.k

Siddu1281, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response