Sum of multiple TimeSpan

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Sum of multiple TimeSpan



I have to do the sum of more time spans in a DataTable to use the code below, but the total sum is wrong, what is due to this:



DataTable(dt) values:


09:21
08:28
08:46
04:23

Total hours: 30,97 //97 minutes is not correct



C# Code:


TimeSpan totaleOreMarcaTempo = TimeSpan.Zero;
int conta = 0;
foreach (DataRow dr in dt.Rows)

String OreMarcaTempo = tm.ConteggioOreGiornaliere(dr["Data"].ToString()); //This string contains at each cycle 09:21 08:28 08:46 04:23
TimeSpan oreMarcatempo = TimeSpan.Parse(OreMarcaTempo.ToString());
totaleOreMarcaTempo = totaleOreMarcaTempo + oreMarcatempo;
conta++;

labelTotaleOreMarcaTempoMod.Text = "" + (int)totaleOreMarcaTempo.TotalHours + ":" + totaleOreMarcaTempo.Minutes.ToString(); //30:58





What would be the correct result you want to achieve?
– Matěj Štágl
Aug 6 at 7:37






exactly what you want as final output?
– Hardik
Aug 6 at 7:38





stackoverflow.com/questions/574881/…
– Mat
Aug 6 at 7:38





Related: stackoverflow.com/questions/4703046/sum-of-timespans-in-c-sharp.
– Tetsuya Yamamoto
Aug 6 at 7:39





I would like to make sure that the sum of the total hours is correct @MatějŠtágl
– riki
Aug 6 at 7:39




5 Answers
5



30.97 is the correct number of hours. It does not mean "30 hours and 97 minutes".



30.97 hours is 30 hours and 58 minutes. 58 / 60 is roughly 0.97.



I think you just need to format your string properly. One way to format it is:


@"(int)yourTimeSpan.TotalHours:yourTimeSpan.Minutes"





58 / 80 is roughly 0.97. should be 58 / 60 is roughly 0.97. :)
– Coskun Ozogul
Aug 6 at 7:49





I tried your solution and the minutes are correct but the hours are 06:58
– riki
Aug 6 at 7:49





@TimSchmelter if I use total hours, I return 30.977777777
– riki
Aug 6 at 7:52





@riki See the edit.
– Sweeper
Aug 6 at 7:53





@Sweeper thanks it's correct
– riki
Aug 6 at 8:01



Value 30.97 is correct (30.97 hours, where 0.97 is hour (60 minutes * 0.97 = 58 minutes),

you just need convert fraction of TotalHours to minutes.


30.97


TotalHours


var raw = "09:21 08:28 08:46 04:23";
var totalTimespan =
raw.Split(" ")
.Select(TimeSpan.Parse)
.Aggregate(TimeSpan.Zero, (total, span) => total += span);

// Use integer value of TotalHours
var hours = (int)totalTimespan.TotalHours;
// Use actual minutes
var minutes = totalTimespan.Minutes


var output = $"hours:minutes";

var expected = "30:58";
output.Should().Be(expected); // Pass Ok





Feel free to comment downvote, will be glad to fix it
– Basin
Aug 6 at 7:52





thanks now it's correct
– riki
Aug 6 at 8:02





@riki, I didn't update code at all ;)
– Basin
Aug 6 at 8:06




You have to change the Format. 0,98 hours = 58,2 minutes


labelTotaleOreMarcaTempoMod.Text =string.Format ("0:00:1:00:2:00",
(int)totaleOreMarcaTempo.TotalHours,
totaleOreMarcaTempo.Minutes,
totaleOreMarcaTempo.Seconds);





This is actually correct answer
– Basin
Aug 6 at 8:17






Feel Free to mark it as the correct answer ;-)
– Mat
Aug 6 at 8:55



30.97 is the correct value but not HH:mm format.



For me the correct solution is :


var total = Math.Floor( totaleOreMarcaTempo.TotalMinutes / 60).ToString() + ":" + Math.Floor( totaleOreMarcaTempo.TotalMinutes % 60).ToString();



To print out a TimeSpan "correctly", just use the correct formatting:


labelTotaleOreMarcaTempoMod.Text = totaleOreMarcaTempo.ToString("c");



or


labelTotaleOreMarcaTempoMod.Text = totaleOreMarcaTempo.ToString("hh':'mm");



EDIT Do note (thanks, Basin) that the second form ignores days.



Reference: Standard TimeSpan Format Strings and Custom TimeSpan Format Strings





This is the correct answer. I don't know why someone has downvoted it.
– George Helyar
Aug 6 at 8:01





@GeorgeHelyar, I am not downvoter, but 1. Format string "hh:mm" will throw FormatException, should be "hh\:mm". 2. totaleOreMarcaTempo.ToString("hh:mm") will not return total hours("30:58") but return "06:58".
– Basin
Aug 6 at 8:15


"hh:mm"


FormatException


"hh\:mm"


totaleOreMarcaTempo.ToString("hh:mm")






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard