C# + Format TimeSpan

后端 未结 10 678
忘掉有多难
忘掉有多难 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 01:58

    The date and time format strings only apply to DateTime and DateTimeOffset. Yo can use a normal format string, though:

    string.Format("{0}:{1:00}", Math.Truncate(duration.TotalMinutes), duration.Seconds)
    

    Note that using TotalMinutes here ensures that the result is still correct when it took longer than 60 minutes.

提交回复
热议问题