Convert seconds to minutes+seconds?

# Convert seconds to minutes+seconds?

• Monday, December 05, 2005 12:21 PM

Sorry for the elementary question, but I am new to C & C#.

I need to convert seconds (upt to 3600 seconds) to minutes & seconds. For example I need to convert 90 seconds to 1 minute and 30 seconds. I'm struggling with how to get and manipulate the remainder after I divide 90 by 60.

Your patience is appreciated...;)

### All Replies

• Monday, December 05, 2005 12:34 PM

minutes = total / 60;

seconds = total % 60;

• Monday, December 05, 2005 8:08 PM

Duh.... Thanks!
• Monday, December 05, 2005 9:41 PM

There is another way to deal with time, you have in .net an object called TimeSpan, that have some nice methods (see the documentation).

Here is an example for getting the time from secconds:
 TimeSpan t = TimeSpan.FromSeconds(90); Console.WriteLine(t.Hours); Console.WriteLine(t.Minutes); Console.WriteLine(t.Seconds); Console.WriteLine(t.ToString());

That code gives this output to the console:
0
1
30
00:01:30
• Monday, December 05, 2005 11:21 PM

bistok,

Excellent! Thanks.

• Friday, September 21, 2007 9:10 PM

Gotta love google. Thanks!
• Thursday, November 06, 2008 7:28 AM

it really helped me
• Wednesday, November 11, 2009 5:45 AM

Sweet, Thanks!
• Monday, May 30, 2011 8:05 PM

Hello Orangy,

I am very new to C# as well and working on a assignment where I'm supposed to write a graphical application that converts an integer number of seconds to an equivalent number of hours and so forth. Can you help me with this process?

• Thursday, July 21, 2011 9:32 AM

Use below  code to convert your seconds to DDD:HH:MM:SS if your seconds are within TimeSpan.MaxValue.

long tick = 3600; //Secods.

TimeSpan timeRemaining = TimeSpan.FromSeconds(tick);

return string.Concat(
timeRemaining.Days > 1 ? string.Concat(timeRemaining.Days, " Days ") : (timeRemaining.Days == 1)? string.Concat(timeRemaining.Days, " Day "):"",
timeRemaining.Hours > 1 ? string.Concat(timeRemaining.Hours, " Hours ") : (timeRemaining.Hours == 1)?  string.Concat(timeRemaining.Hours, " Hour "):"",
timeRemaining.Minutes > 1 ? string.Concat(timeRemaining.Minutes, " Minutes ") : (timeRemaining.Minutes == 1)?  string.Concat(timeRemaining.Minutes, " Minute "):"",
timeRemaining.Seconds > 1 ? string.Concat(timeRemaining.Seconds, " Seconds ") : (timeRemaining.Seconds == 1)? string.Concat(timeRemaining.Seconds, " Second "):""
);

• Thursday, July 21, 2011 12:30 PM

This is not the place to ask questions. Please submit a new question to the forums instead of posting to old ones.