C# + Format TimeSpan

后端 未结 10 702
忘掉有多难
忘掉有多难 2020-12-09 01:44

I am trying to format a TimeSpan element in the format of \"[minutes]:[seconds]\". In this format, 2 minutes and 8 seconds would look like \"02:08\". I have tried a variety

10条回答
  •  渐次进展
    2020-12-09 02:11

    For some mysterious reason TimeSpan never got the ToString() overloads that support formatting until .NET 4.0. For earlier releases, as long as it is positive, you can hijack DateTime.ToString():

    TimeSpan ts = new TimeSpan(0, 2, 8);
    string s = new DateTime(ts.Ticks).ToString("mm:ss");
    

提交回复
热议问题