Seconds to hh mm ss in C#

Published on Monday, July 11, 2011

How to convert seconds number to a string using format “hh:mm: ss”?

Method1:

_int totalTimeLength = 50;_
  
_string timeString = Convert.ToString(TimeSpan.FromSeconds(totalTimeLength));_ 

Method2:

_int intHours = (int) (totalTimeLength / 3600);     
int intMinutes = (int) (totalTimeLength / 60) % 60;      
int intSeconds = (int)totalTimeLength % 60;      
string timeString = intHours .ToString() + ":" + intMinutes .ToString() + ":" + intSeconds .ToString();_