C#将秒数转化为时分秒格式00:00:00

C#将秒数转化为时分秒格式00:00:00

//将秒数转化为时分秒
private string sec_to_hms(long duration)
{
    TimeSpan ts = new TimeSpan(0, 0, Convert.ToInt32(duration));
    string str = "";
    if (ts.Hours > 0)
    {
        str = String.Format("{0:00}", ts.Hours)+":"+  String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds);
    }
    if (ts.Hours == 0 && ts.Minutes > 0)
    {
        str = "00:"+ String.Format("{0:00}", ts.Minutes)+":" + String.Format("{0:00}", ts.Seconds);
    }
    if (ts.Hours == 0 && ts.Minutes == 0)
    {
        str = "00:00:" + String.Format("{0:00}",ts.Seconds);
    }
    return str;
}

结果为:

00:00:10

00:01:10

03:01:10等

你可能感兴趣的:(C#)