TimeSpan格式化为分钟和秒,没有小时

Тол*_*оля 1 c# formatting timespan

我需要格式TimeSpan到分钟和秒(没有小时),我期待MSDN格式,但没有找到任何我可以重用的例子.

我需要的例子:

TimeSpan.FromSeconds(1) --> 00:01
TimeSpan.FromSeconds(60) --> 01:00
TimeSpan.FromSeconds(3600) --> 60:00
TimeSpan.FromSeconds(36000) --> 600:00
Run Code Online (Sandbox Code Playgroud)

任何格式化可能用于将TimeSpan转换为字符串的想法在几分钟内:秒格式?

khe*_*ang 8

试试这个:

var timeSpan = TimeSpan.FromSeconds(3123);

var result = string.Format("{0:D2}:{1:D2}", (int) timeSpan.TotalMinutes, timeSpan.Seconds);

// result = "52:03"
Run Code Online (Sandbox Code Playgroud)