Tuesday, March 6, 2012

Convert Seconds to HH:MM:SS Format:


protected void Page_Load(object sender, EventArgs e)
    {
int seconds = 10000;
       string Time = ConvertToHHMMSS1(seconds);
       //string Time = ConvertToHHMMSS2(seconds);
lblImpCurTimePerday.Text = Time;

    }

private string ConvertToHHMMSS1(int seconds)
    {
        TimeSpan ts = new TimeSpan(0, 0, 0, seconds, 0);
        int Hours = (int)ts.TotalHours;

        return string.Format("{0}:{1}:{2}", Hours, ts.Minutes, ts.Seconds);
    }

private string ConvertToHHMMSS2(int seconds)
  {
    return string.Format("{0:00}:{1:00}:{2:00}", seconds / 3600, (seconds / 60) % 60, seconds % 60);
  }

No comments:

Post a Comment