-3

I have 4 text boxes namely start date,start time,end date and end time. In here for date textboxes I use ajax CalendarExtender to choose the dates and for time textboxes I use MaskedEditExtender to enter the time.

The help I need is how to get the duration based on the values I specify in the textboxes. First I need to get no of days duration based on start and end dates and then I have to calculate the time duration separately based on start and end time.

After completing them in the fifth textbox named Duration the value must be automatically calculated and displayed on it. The main help I need is although the date and time is separately the duration should be calculated only by comparing the date and time and then the final calculated duration should be displayed on the textbox.

I am using Visual Studio 2008 with back-end as SQL Server 2008 and the technology is ASP.net with C# Code.

Nima Derakhshanjan
  • 1,380
  • 9
  • 24
  • 37
Ezhilan
  • 185
  • 3
  • 5
  • 17
  • @Rajesh I haven't tried anything so far. Previously the person who wrote the code has used only time duration calculation based on two time values but i have been assigned to change it to datetime duration calculation. So it would be better if anyone could help me with a brief code explanation. – Ezhilan Jan 22 '15 at 05:04

1 Answers1

2

Try the belowcode

DateTime startTime =Convert.ToDateTime(txtstarttime.Text);
DateTime endTime = Convert.ToDateTime(txtendtime.Text);

DateTime startdate=Convert.ToDateTime(txtstartdate.Text);
DateTime enddate=Convert.ToDateTime(txtenddate.Text);
if(startTime>endTime)
{
 TimeSpan span = endTime.Subtract (startTime);
 if(span!=null)
 {
   string timedifference=Convert.ToString(span.Hours)+" Hours "+Convert.ToString(span.Minutes)+" Minutes "+Convert.ToString(span.Seconds)+" Seconds";
 }
}
if(startdate>enddate)
{
 string datedifference=(enddate- startdate).TotalDays.ToString()+" days ";
}
if(datedifference!=null&&timedifference!=null)
{
  txtDuration=datedifference+ timedifference;
}
Rajesh
  • 1,600
  • 5
  • 33
  • 59