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 : 2910 | Status : [Member] | Replies : 4
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

Posted by: Satyapriyanayak on: 1/24/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Total_days_from_two_dates_csharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

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;
}
}
}


If this post helps you mark it as answer
Thanks

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

Login to post response