-1

Below is my user interface:

I want to calculate total time of class in ASP.net:

UI

I have tried but not working properly and give error:

Incorrect syntax near ')'.

string[] Start = StartTime.Text.Split(':');
string[] End = EndTime.Text.Split(':');

Starthours = int.Parse(Start[0]);
StartMinuts = int.Parse(Start[1]);

Endhours = int.Parse(End[0]);
EndMinuts = int.Parse(End[1]);

for (int i = Starthours; i != Endhours; i++)
{
    hours++;
}
for (int i = StartMinuts; i != EndMinuts; i++)
{
    if (minuts > 60)
    {
       hours++;
    }
    else
    {
       hours--;
    }
}

total = hours.ToString() + ":" + minuts.ToString();
Supriya
  • 290
  • 4
  • 15
Nouman
  • 11
  • Use `DateTime` objects with `TimeSpan` and work with that. There are [plenty of examples](https://www.google.nl/search?ei=AI9kWvviAczFwAKVm4rYAQ&q=timespan+c%23+site%3Astackoverflow.com&oq=timespan+c%23+site%3Astackoverflow.com&gs_l=psy-ab.3...1491.7848.0.8029.33.28.5.0.0.0.148.2034.22j6.28.0....0...1c.1.64.psy-ab..0.4.352...0j0i22i10i30k1j0i22i30k1j33i160k1.0.uNv-lHtbuUE) – VDWWD Jan 21 '18 at 13:02
  • [this will help you](https://www.codeproject.com/Questions/334475/Get-the-number-of-hours-between-two-time-in-Csharp) a simple but cool one – Syed Muhammad Munis Ali Jan 21 '18 at 20:01

1 Answers1

0

You could try this:

string[] Start = StartTime.Text.Split(':');
string[] End = EndTime.Text.Split(':');

var startTime = new DateTime(1, 1, 1, Int32.Parse(Start[0]), Int32.Parse(Start[1]), 0);
var endTime = new DateTime(1, 1, 1, Int32.Parse(End[0]), Int32.Parse(End[1]), 0);

var timeTaken = endTime - startTime;
var hours = (int)timeTaken.TotalHours;
var minutes = (int)timeTaken.TotalMinutes % 60;

var total = $"{hours}:{minutes}";
SBFrancies
  • 3,987
  • 2
  • 14
  • 37