Page 1 of 1

How to add hours and minutes together in C#

Posted: Sun Nov 29, 2009 2:21 am
by Neo
Sum set of records in a database in HH:MM format and show the result in the same format

Code: Select all

long totalMinutes = 0;
foreach (DataRow row in dtLessHrs.Rows) 
{
  string[] split = row[0].ToString().Split(':');
  totalMinutes += (Int64.Parse(split[0]) * 60) + (Int64.Parse(split[1]));
}
lblTotalLessHrs.Text(String.Format("Total Less Hours = {0:00}:{1:00} (hours/minutes)", 
totalMinutes / 60, (totalMinutes % 60)));