# How to convert seconds to HH:MM:ss format

### Question

• Write a C# program that prompts the user to input the elapsed time for an event in seconds. The program then outputs the elapsed time in hours, minutes, and seconds. (for example, if the elapsed time is 9630 seconds, then the output is 2:40:30.)

Can anyone pls help me!!
Im using visual studio 2005

Monday, November 05, 2007 12:20 PM

• Simple solution would be:

int seconds = 10000; //or whatever time you have
string.Format("{0:00}:{1:00}:{2:00}",seconds/3600,(seconds/60)%60,seconds%60);

would output:

"02:46:40"

if you'd like 2:46:40 you will need to change first item in format string from {0:00} to {0}

of course instead of dividing the seconds by 60 and 3600 you could use TimeSpan or DateTime classes, and get minutes/hours from there.

-Juraj

Monday, November 05, 2007 1:15 PM

### All replies

• Simple solution would be:

int seconds = 10000; //or whatever time you have
string.Format("{0:00}:{1:00}:{2:00}",seconds/3600,(seconds/60)%60,seconds%60);

would output:

"02:46:40"

if you'd like 2:46:40 you will need to change first item in format string from {0:00} to {0}

of course instead of dividing the seconds by 60 and 3600 you could use TimeSpan or DateTime classes, and get minutes/hours from there.

-Juraj

Monday, November 05, 2007 1:15 PM
• Code Block

TimeSpan span = new TimeSpan(0,0,10000);

Console.WriteLine("{0:0}{1:00}{2:00}",span.Hours,span.Minutes,span.Seconds);

Monday, November 05, 2007 6:19 PM
• Thank a lot for your suggestion, here it works.

But still than i have some problem while populating GridView

I ahve the query from the table as follows...

cmd = "select subscriber_id as Subscriber_no,other_party as Dialed_no,duration from TableName";

My objective is in the above query the duration comes from database as no. of seconds.

I want to display in my GridView as hh:mms format(00:21:30).

Note: I am not using boundcoloumn property in GridView.

Thursday, November 08, 2007 7:00 AM
•

Code Block

TimeSpan span = new TimeSpan(0,0,10000);

Console.WriteLine("{0:0}{1:00}{2:00}",span.Hours,span.Minutes,span.Seconds);

that's great. thanks so much .
Tuesday, August 31, 2010 4:30 AM