# how to convert seconds in min:sec format

how to convert seconds in Minute:Second format

``````var seconds = 60;
//10,000 ticks in a millisecond
var ticks = seconds*10000*1000;
DateTime dt = new DateTime(ticks);
dt.ToString("mm:ss");
``````
``````int totalSeconds = 222;
int seconds = totalSeconds % 60;
int minutes = totalSeconds / 60;
string time = minutes + ":" + seconds;
``````

Simple maths. 60 seconds to a minute.

``````int mins = totalseconds/60;
int secs = totalseconds % 60;

Console.Writeline(string.Format("{0}:{1}", mins, secs));
``````

Something like this:

``````string minSec = string.Format("{0}:{1:00}", seconds / 60, seconds % 60);
``````

Note that that will ensure the seconds are always displayed as two digits, e.g. "2:05" for 125 seconds. The minutes aren't currently treated the same way, but of course they could be.

This doesn't deal well with negative numbers. If your seconds may be negative, you may want something like this:

``````string minSec = string.Format("{0}:{1:00}", seconds / 60,
(Math.Abs(seconds)) % 60);
``````

Finally, will you always have less than an hour of seconds? It might look odd to see "80:00" when you really mean "1:20:00" for example.

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);
``````

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"));
``````

An alternative way to display the seconds as e.g. "2:05" could be to use PadLeft.

``````string time = minutes.ToString() + ":" + seconds.ToString().PadLeft(2,'0');
``````
``````double seconds=125;
TimeSpan.FromSeconds(seconds).ToString()
``````

will give you : `00:02:05`. As per my understanding this built-in solution is more extensible, since it can give you hours too, without any plumbing of the logic.

``````public static string SecondsToMinutes(int seconds)