System.DateTime oneSecond = new DateTime(0);

oneSecond = oneSecond.AddSeconds(1);

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

oneSecond.ToShortTimeStrin

Regards

Solved

Posted on 2005-04-26

Hi,

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

Thanks,

Yama

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

Thanks,

Yama

13 Comments

System.DateTime oneSecond = new DateTime(0);

oneSecond = oneSecond.AddSeconds(1);

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

oneSecond.ToShortTimeStrin

Regards

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

Anyway, try

oneSecond.ToLongTimeString

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

Regards

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

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

{

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.To

}

works as well.

Cheers,

Greg

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

========================

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();

}

Here is a better way of doing this convertion:

public string GetStringTimeFromSeconds(i

{

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

int hr = Math.Floor(seconds/3600) ;

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

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

Yama

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..T

}

By clicking you are agreeing to Experts Exchange's Terms of Use.

Title | # Comments | Views | Activity |
---|---|---|---|

C# / ASP.NET - Custom Server Control - Property does not seem to keep setting. | 3 | 28 | |

deploying a service to production environment | 13 | 64 | |

How to catch SQL Errors from ADO.NET | 3 | 79 | |

report c# | 9 | 56 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**8** Experts available now in Live!