# Convert seconds to hours:minute:second

Hi,

Would like to convert seconds to hours:minutes:seconds as such that 1 second = 00:00:01.

Thanks,

Yama
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Hi yamazed,

System.DateTime oneSecond = new DateTime(0);

If you then need to format it as 00:00:01, you can use:

oneSecond.ToShortTimeString();

Regards
0
Author Commented:
Hi,

Try to convert 7200 seconds to HH:mm:ss using your lines of code and you will get 2:00 AM!! That is not right.

Yama
0
Commented:
Well, it's not that far wrong either. I mean, 7200 seconds is 2 hours.

Anyway, try

oneSecond.ToLongTimeString();

And that should give you 02:00:00 as desired.

Regards
0
Author Commented:
Hi,

Well try 215115 seconds and you'll get 11:45:15 or 7777777777 will not give you correct hours:minutes:seconds because it only seems to parse a 24 hour clock.

What I desire is to get the actually hours:minutes:seconds from any given seconds integer.

Thanks,

Yama
0
Commented:
hmm seem to me to be a much easier way ...

int hours = Math.Floor(TotalSeconds / 3600); // 60x60 give total hour parts
int minutes = Math.Floor((TotalSeconds - (hours * 3600)) / 60); //60 seconds
int seconds = TotalSeconds - hours * 3600 - minutes * 60
0
Commented:
note I just typed it in quickly here, no compiler at this second you could just int math and allow the int to knock off the remainders instead of using round
0
Commented:
static void PrintSeconds(int TotalSeconds)
{
TimeSpan t = new TimeSpan(0,0,TotalSeconds);
int hours = (int) t.TotalHours;
int minutes = (int) t.Minutes;
int seconds = (int) t.Seconds;
Console.WriteLine(hours.ToString() + ":" + minutes.ToString() + ":" + seconds.ToString());
}

works as well.

Cheers,

Greg
0
Commented:
Hi Yamazed,

From the way you phrased your original question i.e. you listed the time as 00:00:01, I presumed (based on the use of 2 digits for the hours) that this would be an actual time of day, hence the use of the datetime structure as a quick and easy solution. If it's an arbitrary number of hours, then by all means, go with gregoryyoung's solution.

Regards
0
Commented:
Optimize the logic if u want... e.g. Use string builder etc
========================

public string GetStrTimeFromSces(int secs)
{

int hours = secs/3600;
int mins= (secs - hours*3600)/60;
secs = secs - hours*3600 - mins * 60;

string strSecs = secs.ToString();
string strMins = mins.ToString();
string strHours = hours.ToString();

if (secs < 10)  strSecs = "0" + strSecs;
if (mins < 10) strMins = "0" + strMins;
if (hours < 10) strHours = "0" + strHours;

return  strHours.ToString() +":"+ strMins.ToString() +":"+ strSecs.ToString();
}
0
Author Commented:
Hi Nachiket,

Here is a better way of doing this convertion:

public string GetStringTimeFromSeconds(int seconds)
{
string newTime ;
try
{
int hr = seconds/3600 ;
int min = (seconds % 3600)/60 ;
int sec = (seconds % 3600) % 60 ;

string shr = hr.ToString() ;
string smin = min.ToString() ;
string ssec = sec.ToString() ;

if (hr < 10)
shr = "0" + shr ;

if (min < 10)
smin = "0" + smin ;

if (sec < 10)
ssec = "0" + ssec ;

newTime = shr + ":" + smin  + ":" + ssec ;
}
catch(Exception)
{
newTime = "00:00:00";
}

return newTime ;
}

Yama
0
Author Commented:
Actually just one small change:

int hr = Math.Floor(seconds/3600) ;
int min = Math.Floor((seconds % 3600)/60) ;
int sec = Math.Floor((seconds % 3600) % 60) ;

Yama
0
Commented:
I personally prefer using the timespan object but hey thats just me :)

also instead of going through all the ifs why not just use the ToString() method of the int ...

thus ending up with

static void PrintSeconds(int TotalSeconds)
{
TimeSpan t = new TimeSpan(0,0,TotalSeconds);
int hours = (int) t.TotalHours;
int minutes =  t.Minutes;
int seconds =  t.Seconds;
Console.WriteLine(hours..ToString("0#") + ":" + minutes.ToString("0#") + ":" + seconds.ToString("#0"));
}

0

Experts Exchange Solution brought to you by