how to convert seconds in Minute:Second format
There are 60 seconds in every minute, so converting seconds to minutes is simple. Just divide the number of seconds by 60 to get your answer!
As with Excel, the first step to converting elapsed second to time is to divide the value by 86400. To format the cells for mm:ss, select Format > Number > More Formats > More date and time formats from the Menu.
A versatile version is to use TimeSpan
like this:
var span = new TimeSpan(0, 0, seconds); //Or TimeSpan.FromSeconds(seconds); (see Jakob C´s answer) var yourStr = string.Format("{0}:{1:00}", (int)span.TotalMinutes, span.Seconds);
int totalSeconds = 222;
int seconds = totalSeconds % 60;
int minutes = totalSeconds / 60;
string time = minutes + ":" + seconds;
Just for completeness I will add an answer using TimeSpan
(works as of .NET 4.0):
int seconds = 1045;
var timespan = TimeSpan.FromSeconds(seconds);
Console.WriteLine(timespan.ToString(@"mm\:ss"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With