Solved

# Convert seconds to hours:minute:second

Posted on 2005-04-26
425 Views
Hi,

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

Thanks,

Yama
0
Question by:yamazed

LVL 5

Expert Comment

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 Comment

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

LVL 5

Expert Comment

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 Comment

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

LVL 37

Expert Comment

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

LVL 37

Expert Comment

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

LVL 37

Expert Comment

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

LVL 5

Expert Comment

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

LVL 4

Expert Comment

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 Comment

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 Comment

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

LVL 37

Accepted Solution

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

LVL 96

Expert Comment

I like the TimeSpan method, too :)

Bob
0

## Featured Post

### Suggested Solutions

In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…