In my application I have 4 TextBoxes, and 2 TextBoxes to enter the start-time, and 2 TextBoxes to enter end-time.
The user will always enter a completed time, so the input will always be 11:30, 12:45, and so on.
How can I get the difference in hours and minutes between start and endtime?
To calculate the number of hours between two times:Subtract the starting time from the ending time. Do you have any seconds or minutes after the subtraction? If so: Take seconds, divide them by 60, and add the result to the minutes.
To calculate the total minutes between two times: Make sure to convert both times to 24-hour time format. Take starting time and subtract it from ending time. Multiply the hours by 60 to convert it into minutes and add it to any amount of minutes you have left after the subtraction.
use TimeSpan, no need to use dates
var start = TimeSpan.Parse(start.Text);
var end = TimeSpan.Parse(end.Text);
TimeSpan result = end - start;
var diffInMinutes = result.TotalMinutes();
Use TimeSpan class, and Subtract method of DateTime.
DateTime t1 = Convert.ToDateTime(textBox1.Text);
DateTime t2 = Convert.ToDateTime(textBox2.Text);
TimeSpan ts = t1.Subtract(t2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With