-1

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

  1. Textbox1 for Time entry.
  2. 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?

Itay
  • 16,601
  • 2
  • 51
  • 72

1 Answers1

2
  1. Convert TextBox1.Text to System.DateTime entry
  2. Convert TextBox2.Text to System.DateTime leave
  3. Calculate Timespan t = leave - entry - Timespan.FromMinutes(30)
  4. 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);
Community
  • 1
  • 1
Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91