0

I am downloading the list of times from the database

var listaNadgodzinZPoprzMies = _ecpContext.Karta.Where(x => x.Login == userName && x.Rok == numerRoku && x.Miesiac < numerMiesiaca)
                                                            .Select(b => string.IsNullOrEmpty(b.SaldoNadgodzin) ? TimeSpan.Zero : TimeSpan.Parse(b.SaldoNadgodzin))
                                                            .ToList();

Adds all times

var sumaListaNadgodzinZPoprzMies = listaNadgodzinZPoprzMies.Aggregate(TimeSpan.Zero, (t1, t2) => t1 + t2);

And, I need to convert the number of minutes/ from the variable shown in the image (TimeSpan)

to string to format HHH:mm to other new variable

enter image description here

I scraped out these two functions some time ago in javascript ( I don't know how to convert them to c # ) (I don't know if it will work and whether it will be useful)

function msToTime(duration) {
    const minutes = Math.floor((duration / (1000 * 60)) % 60), // 3.4 - 3, 3.5 - 3, 3.8 - 3
        hours = Math.floor(duration / (1000 * 60 * 60));
    return twoOrMoreDigits(hours) + ":" + twoOrMoreDigits(minutes);
}

function twoOrMoreDigits(n) {
    return n < 10 ? '0' + n : n; // if (n < 10) { return '0' + n;} else return n;
}

anyone have any idea?

  • you need convert timeSpan to DateTime like format 'hh:mm' or any format time ? if you need this, please see link https://stackoverflow.com/a/13044648/12777835 – Zanyar Jalal Feb 11 '20 at 13:57

2 Answers2

1

Here is an example:

TimeSpan time = new TimeSpan(200,1,23);

string strTime = $"{((int)time.TotalHours).ToString("D3")}:{time.Minutes.ToString("D2")}";

Console.WriteLine(strTime);

Output:

200:01

Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
-1

You want to format a timespan, you can achieve it by using this code:

var timespan = TimeSpan.FromMinutes(3180);
var result = timespan.ToString("hh:mm");
Console.WriteLine(result);

hh - hour in 24h format with leading zero
mm - minutes with leading zero

You can read more about timespan formatting here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-timespan-format-strings

Tal
  • 548
  • 3
  • 8