How to convert seconds to hh:mm:ss format in csharp

In this blog we will know how to convert seconds to hh:mm:ss format in csharp.

 

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            MessageBox.Show(ConvertToTime(90640));

        }

        public string ConvertToTime(double timeSeconds)

        {

            int mySeconds = System.Convert.ToInt32(timeSeconds);

 

            int myHours = mySeconds / 3600; //3600 Seconds in 1 hour

            mySeconds %= 3600;

 

            int myMinutes = mySeconds / 60; //60 Seconds in a minute

            mySeconds %= 60;

 

 

            string mySec = mySeconds.ToString(),

            myMin = myMinutes.ToString(),

            myHou = myHours.ToString();

 

            if (myHours < 10) { myHou = myHou.Insert(0, "0"); }

            if (myMinutes < 10) { myMin = myMin.Insert(0, "0"); }

            if (mySeconds < 10) { mySec = mySec.Insert(0, "0"); }

 

 

            return myHou + "hh"+":" + myMin + "mm"+":" + mySec +"ss";

        }

    }

}

 

 

Output

 

25hh:10mm:40ss