Try this:
var timeSpan = TimeSpan.FromSeconds(3123);
var result = string.Format("{0:D2}:{1:D2}", (int) timeSpan.TotalMinutes, timeSpan.Seconds);
// result = "52:03"
Pregunta
I need format TimeSpan to minutes and seconds(without hours), I looks to MSDN formatting but not found any example which I can reuse.
Example of what I need:
TimeSpan.FromSeconds(1) --> 00:01
TimeSpan.FromSeconds(60) --> 01:00
TimeSpan.FromSeconds(3600) --> 60:00
TimeSpan.FromSeconds(36000) --> 600:00
Any ideas which format possible use to convert TimeSpan to string in minutes:seconds format?
Solución
Try this:
var timeSpan = TimeSpan.FromSeconds(3123);
var result = string.Format("{0:D2}:{1:D2}", (int) timeSpan.TotalMinutes, timeSpan.Seconds);
// result = "52:03"
Otros consejos
You can use TimeSpan.TotalMinutes
for the first part and also pad the number of seconds with a custom format string:
var formatted = string.Format("{0}:{1:D2}", (int)ts.TotalMinutes, ts.Seconds);
For those who prefer to just pass string format
timespan.ToString("mm\\:ss");