# Convert seconds to minutes+seconds?

### Question

• 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...;)
Monday, December 05, 2005 12:21 PM

### Answers

• 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 9:41 PM
• minutes = total / 60;

seconds = total % 60;

Monday, December 05, 2005 12:34 PM

### All replies

• minutes = total / 60;

seconds = total % 60;

Monday, December 05, 2005 12:34 PM
• Duh.... Thanks!
Monday, December 05, 2005 8:08 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 9:41 PM
• bistok,

Excellent! Thanks.

Monday, December 05, 2005 11:21 PM
• Gotta love google. Thanks!
Friday, September 21, 2007 9:10 PM
• it really helped me
Thursday, November 06, 2008 7:28 AM
• Sweet, Thanks!
Wednesday, November 11, 2009 5:45 AM
• 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?

Monday, May 30, 2011 8:05 PM
• 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 9:32 AM
• This is not the place to ask questions. Please submit a new question to the forums instead of posting to old ones.
Thursday, July 21, 2011 12:30 PM