Convert seconds to hhh:mm:ss in a chart

后端 未结 3 1859
南旧
南旧 2021-01-22 06:19

I have a MsSql database which calculates the timespan between two dates in seconds. That works fine. I use this column afterwards in C# and write them in an array.

This

3条回答
  •  滥情空心
    2021-01-22 06:25

    If you are trying to show more than 2 digits of hour I think this should work for you

    //yourTimeSpan is the TimeSpan that you already have
    var hoursDouble = Math.Floor(yourTimeSpan.TotalHours);
    string hours;
    string minutes;
    string seconds;
    
    //check hours
    if(hoursDouble < 10)
    {
        hours = string.Format("0{0}", hoursDouble);
    }
    else
    {
        hours = hoursDouble.ToString();
    }
    //check minutes
    if (yourTimeSpan.Minutes < 10)
    {
        minutes = string.Format("0{0}", yourTimeSpan.Minutes);
    }
    else
    {
        minutes = yourTimeSpan.Minutes.ToString();
    }
    //check seconds
    if (yourTimeSpan.Seconds < 10)
    {
        seconds = string.Format("0{0}", yourTimeSpan.Seconds);
    }
    else
    {
        seconds = yourTimeSpan.Seconds.ToString();
    }
    
    string formattedSpan = String.Format("{0}:{1}:{2}", hours, minutes, seconds);
    

    Update: I think this should solve the problem you were seeing with single digit numbers

提交回复
热议问题