Home > Net >  Get total hours in a foreach loop?
Get total hours in a foreach loop?

Time:04-20

I have a foreach loop that shows each employee's clock in and clock out on a work order. I've been able to calculate and show those time as a total of hours/minutes/seconds. I used the following code.

@if(employeehour.ClockOut != null)
{                               
    TimeSpan duration = DateTime.Parse(employeehour.ClockOut.ToString()).Subtract(DateTime.Parse(employeehour.ClockIn.ToString()));

    double totalHours = duration.TotalHours;
    calculateHours.Add(totalHours);
    string formatted = string.Format("{0}{1}{2}{3}",
                           duration.Duration().Days > 0 ? string.Format("{0:0} day{1} ", duration.Days, duration.Days == 1 ? string.Empty : "s") : string.Empty,
                           duration.Duration().Hours > 0 ? string.Format("{0:0} hour{1} ", duration.Hours, duration.Hours == 1 ? string.Empty : "s") : string.Empty,
                           duration.Duration().Minutes > 0 ? string.Format("{0:0} minute{1} ", duration.Minutes, duration.Minutes == 1 ? string.Empty : "s") : string.Empty,
                           duration.Duration().Seconds > 0 ? string.Format("{0:0} second{1} ", duration.Seconds, duration.Seconds == 1 ? string.Empty : "s") : string.Empty);
    <p>@formatted</p>
}

@formatted gives the result as shown in the following image.

enter image description here

I want to total the formatted hours of each row in my foreach, ultimately to get the 'Total Man Hours' worked on a project.

I need a total of all hours in the Hours column. Any thought on this? I need this outside of the foreach loop.

CodePudding user response:

In your code the calculateHours would just get the hours. I am assuming if 2 people worked 2.5 hours you would want your total hours to be 5 total man hours not 4.

Initialize a TimeSpan outside of your for loop

TimeSpan totalTime = new TimeSpan();

and then add your duration in each iteration

totalTime = totalTime.Add(duration);

then use your format the same way after the loop

CodePudding user response:

I removed the logic from the foreach loop and moved it to it's own function.

private List<double> calculateHours = new List<double>();
private double calculatedHours;
private string formattedTotalHours;
private string formattedTotalMinutes;

private void calculateTotalHours()
{
    calculateHours.Clear();

    foreach(var e in employeehours){

        TimeSpan duration = DateTime.Parse(e.ClockOut.ToString()).Subtract(DateTime.Parse(e.ClockIn.ToString()));

        double totalHours = duration.TotalHours;
        calculateHours.Add(totalHours);
    }

    calculatedHours = calculateHours.Sum();
    formattedTotalHours = TimeSpan.FromHours(calculatedHours).ToString(@"hh");
    formattedTotalMinutes = TimeSpan.FromHours(calculatedHours).ToString(@"mm");
}

and in my Razor code:

<p><b>Total Man Hours:</b> @formattedTotalHours hours @formattedTotalMinutes minutes</p>

enter image description here

  • Related