ASP.NET - time calculation

Asked By kiran Kumar on 20-Jan-12 07:55 AM
hi,

 in my asp website i have placed 3 textbox controls and one button [save] for entering the employee attendance. and calculating the number of working hours

  textbox1-->  in time in hours
  textbox 2--> out time  in hours  

 textbox 3 --> total working hours    [textbox2 - textbox1]
  

 For example, if the in time of an employee is  9:30 am and out time is 6:30 pm, then the total work hours will be 9hours.
 This 9 hrs will be displayed in the textbox 3 as [ textbox2- textbox1]

 How to do this?? i want to write it in the textbox2 textchanged event . Give me the code please
Web Star replied to kiran Kumar on 20-Jan-12 08:03 AM
  1. DateTime firstDateTime = DateTime.Now;  
  2.     DateTime secondDateTime = DateTime.Now.AddDays(3);  
  3.     TimeSpan dateTimeDifference;  
  4.     dateTimeDifference = secondDateTime.Subtract(firstDateTime);  
Sreekumar P replied to kiran Kumar on 20-Jan-12 08:38 AM
Hi,

You can this code to get the difference in TimeSpan object.

DateTime firstDateTime = DateTime.Parse("9:30 AM");
DateTime secondDateTime = DateTime.Parse("6:30 PM");
TimeSpan dateTimeDifference;
dateTimeDifference = secondDateTime.Subtract(firstDateTime);
dateTimeDifference.Hours.ToString();

Danasegarane Arunachalam replied to kiran Kumar on 20-Jan-12 02:41 PM
And the simple method Will be


Dim intHours as Integer=CDate(textbox2.text)-CDate(TextBox1.Text).Hours
Devil Scorpio replied to kiran Kumar on 22-Jan-12 03:20 PM
Hi,

As the time string don't have AM or PM the conversion will take it as a 24 hr clock

You need to convert the time in 24 hr format
9:30 am = 9:30
6:30 pm = 18:30

DateTime dateTime1 = Convert.ToDateTime("18:30");
DateTime dateTime2 = Convert.ToDateTime("9:30");
TimeSpan timeSpan=dateTime1.Subtract(dateTime2);