Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert seconds in min:sec format

Tags:

c#

time

how to convert seconds in Minute:Second format

like image 651
Ramesh Avatar asked Sep 08 '10 06:09

Ramesh


People also ask

What is the formula to convert seconds to minutes?

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!

How do you convert seconds to HH mm SS format in Excel?

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.


3 Answers

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); 
like image 199
Lasse Espeholt Avatar answered Sep 22 '22 03:09

Lasse Espeholt


int totalSeconds = 222;
int seconds = totalSeconds % 60;
int minutes = totalSeconds / 60;
string time = minutes + ":" + seconds;
like image 31
x2. Avatar answered Sep 23 '22 03:09

x2.


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"));
like image 42
Jakob Christensen Avatar answered Sep 20 '22 03:09

Jakob Christensen