How can I use timespan for each row of the sql table and sum it all up afterwards?

StackOverflow https://stackoverflow.com/questions/22273291

  •  11-06-2023
  •  | 
  •  

Question

I'm doing payroll system log-in and log-out system. To get the interval between two different time I used timespan but I need to use it in every single row of an employee so that I could add it all up for the whole month. This is my code for getting the timespan of a specific row:

             DateTime dt1 = Convert.ToDateTime(label9.Text);
             DateTime dt2 = Convert.ToDateTime(label10.Text);
             TimeSpan total = dt2 - dt1;

Label9.Text and Label10.Text is invisible in the forms, whenever I click a specific row in the gridview the time in and time out of the specific row will insert to that labels so I could get the timespan.

Était-ce utile?

La solution

Summing TimeSpans in c#, one way is

TimeSpan totalTime = new TimeSpan(0);

foreach(var something in somethings)
{
   totalTime = totalTime.Add(new TimeSpan(something.end - something.start);
}

Autres conseils

To summing datetimes to timespan, you can use something like,

string startTime = "7:00 AM";
string endTime = "2:00 PM";

TimeSpan duration = DateTime.Parse(endTime).Subtract(DateTime.Parse(startTime));
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top