.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> DateTime >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Convert seconds to Hour Minute Second

Posted By:satyapriyanayak       Posted Date: October 07, 2013    Points: 40    Category: DateTime    URL: http://www.dotnetspark.com  

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


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend