Q:

Calculating total numbersof hours for attendance

Am Working on Attendance system in asp.net, I have two labels and Textboxes:

Textbox1 for Time entry.  
Textbox2 for leave entry.

Both are inputted by user in terms for example: Entry time is: (7:50 AM) and the Leave time is: (6:15 PM).
Now I want to calculate the total numbers of hours for the inputted entries.
keeping in mind in the day he/she has 30 minutes break. So how to do that?

A:

Convert TextBox1.Text to System.DateTime entry
Convert TextBox2.Text to System.DateTime leave
Calculate Timespan t = leave - entry - Timespan.FromMinutes(30)
Get t.TotalHours()

For converting time string into DateTime use DateTime.ParseExact ( Why can’t DateTime.ParseExact() parse the AM/PM in “4/4/2010 4:20:00 PM” using “M'/'d'/'yyyy H':'mm':'ss' 'tt” )
    var ci = new CultureInfo("en-US");

    DateTime entry = DateTime.ParseExact("7:50 AM", "h:mm tt", ci);
    DateTime leave = DateTime.ParseExact("6:15 PM", "h:mm tt", ci);

    TimeSpan t = leave - entry - TimeSpan.FromMinutes(30);

    var totalTime = string.Format("{0:D2}h:{1:D2}m", t.Hours, t.Minutes);

