Home > OS >  Sum times over 24:00 in C#
Sum times over 24:00 in C#

Time:08-18

I want to collect a few hours, but if sum is over 24:00 I take as like it: 1.01:20

How can it in c# or c# with SQL: 23:00 02:00 = 25:00 ?

Best regars

CodePudding user response:

What has this question to do with Mysql at all? You are asking about the sum of multiple C# TimeSpan, aren't you? Then TotalHours might give you the answer:

TimeSpan ts = TimeSpan.FromHours(23);
ts = ts   TimeSpan.FromHours(2);
int hours = (int) ts.TotalHours;

If you want the sum as formatted string "25:00", use this approach:

string hourMinute = $"{((int)ts.TotalHours).ToString("D2")}:{ts.Minutes.ToString("D2")}";

The ToString("D2") ensures that you always have at least two digits for the hours and minutes, with a leading zero if necessary. Read: Standard numeric format strings

  • Related