-3

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#: 23:00 + 02:00 = 25:00 ?

Best regards

  • 1
    Do you want it done in a SQL query or do you want to do in C# code? If the former then the C# tag is irrelevant and, if the latter, the MySQL tag is irrelevant. It's up to you to decide and specify which one, because questions need to be specific. – John Aug 17 '22 at 10:34
  • Hi, My purpose in putting the SQL tag, maybe there is a way to do this in direct SQL. For example, it collects the clocks directly in a colum. and I repleced Mysql to SQL in my question Best regards – Aykut Günhan Aug 18 '22 at 05:41
  • I'm sure you could do that also in sql, if you're still interested it would be better to ask another question. But then also show your query and the table. You can edit this question to remove the sql part of it. It was also better to provide some own code, so people can see what you have tried. – Tim Schmelter Aug 18 '22 at 07:04
  • okey, I get it, thank you – Aykut Günhan Aug 18 '22 at 12:39

1 Answers1

7

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

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Yes you are right about Mysql soryy. Thank you for your answer. I got it! TotalHours give me the my answer. Thank you again. – Aykut Günhan Aug 17 '22 at 10:47