The starting time is entered in textBox1 - 8:55 (or 08:55)
The final time is entered in textBox2 - 9:02 (or 09:02)
QUESTION
How to calculate the duration and add to textBox3 ?
In this case, it should work out - 7 minutes.
//получаем данные string inputString1 = textbox1.Text; string inputString2 = textbox2.Text; //конвертируем данные TimeSpan time1, time2; if (!TimeSpan.TryParse(inputString1, out time1)) { //выводим пользователю ошибку о том что пусть нормально введет время в textbox1 } if (!TimeSpan.TryParse(inputString2, out time2)) { //выводим пользователю ошибку о том что пусть нормально введет время в textbox2 } //Выводим данные пользователю в формате hh:mm textbox3.Text = (time2 - time1).ToString(@"hh\:mm"); //Либо, как подсказали из коментариев, выводим интервал только в минутах textbox3.Text = (time2 - time1).TotalMinutes.ToString("###############"); textbox3.Text = (time2 - time1).TotalMinutes.ToString("###############"); - 4perYou can try through TimeSpan . Only at the beginning you have to convert the lines to DateTime .
DateTime oldDate = new DateTime(2016, 11, 25, 11, 25, 0); DateTime newDate = new DateTime(2016, 11, 25, 11, 28, 0); TimeSpan ts = newDate - oldDate; int differenceInMins = ts.Minutes; The resulting result is converted to a string and textBox to the textBox . If a difference of more than 60 minutes is allowed, use the appropriate TimeSpan members. For example:
int differenceInMins = (ts.Hours*60)+ts.Minutes; TimeSpan simply involved and that's it. The principle itself remains unchanged. - StreletzSee the TimeSpan structure. Algorithm for solving the problem:
DateTime data type.TimeSpan . Its value will be equal to the time difference.textbox end result.Source: https://ru.stackoverflow.com/questions/595842/
All Articles
TextBoxuse theDateTimePickerto enter the time. - Alexander Petrov