How to add hours and minutes together in C#

.NET programming topics
Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to add hours and minutes together in C#

Post by Neo » Sun Nov 29, 2009 2:21 am

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)));
Post Reply

Return to “.NET Programming”