none
Convert total minutes to hours and minutes in C# winform RRS feed

  • Question

  • Here is the code that I tried, I am new to c# and am trying to teach myself. Could someone please help me with this code, so far I have not been able to get it to work? 

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
     
    namespace HouseHold
    {
        public partial class convertMinutes : Form
        {
            public convertMinutes()
            {
                InitializeComponent();
            }
     
            private void timer1_Tick(object senderEventArgs e)
            {
                timer1.Interval = 1000;
                lblDate.Text = DateTime.Now.ToLongDateString();
                lblTime.Text = DateTime.Now.ToLongTimeString();
            }
     
            private void btnProcess_Click(object senderEventArgs e)
            {
                Int32 totalMinute = default(Int32);
                Int32 Minute = default(Int32);
                Int32 Hour = default(Int32);
                {
                    txtBxTotal.Text = totalMinute.ToString();
                    totalMinute = totalMinute % 1440;
     
                    Hour = totalMinute / 60;
                    Minute = totalMinute % 60;
                    txtBxTotal.Text = FormatTwoDigits(Hour+ " : " + FormatTwoDigits(Minute+ " ";
                }
            }
            private string FormatTwoDigits (Int32 i)
            {
                string functionReturnValue = null;
                if (10 > i)
                {
                    functionReturnValue = "0" + i.ToString();
                }
                else
                {
                    functionReturnValue = i.ToString();
                }
                return functionReturnValue;
            }
            
            private void btnClear_Click(object senderEventArgs e)
            {
                txtBxTotal.Text = "";
                txtBxTotalMin.Text = "";
            }
            
            private void btnExit_Click(object senderEventArgs e)
            {
                Close();
            }
        }
    }
    


    ynot333

    Sunday, August 3, 2014 8:34 PM

Answers

  • You could first convert the text in the "Total Minutes" TextBox to an int, then convert this int to a TimeSpan and then you can access the Hours and Minutes properties of the TimeSpan object to get the hours and minutes:

            private void btnProcess_Click(object sender, EventArgs e)
            {
                int totalMinutes = Convert.ToInt32(txtBxTotalMin.Text);
                TimeSpan ts = TimeSpan.FromMinutes(totalMinutes);
                txtBxTotal.Text = string.Format("{0} : {1}", ts.Hours, ts.Minutes);
            }
    Please remember to mark any helpful posts as answer and/or helpful.
    • Proposed as answer by Pantelis44999 Sunday, August 3, 2014 11:21 PM
    • Marked as answer by ynot333 Monday, August 4, 2014 2:30 AM
    Sunday, August 3, 2014 8:55 PM

All replies

  • You could first convert the text in the "Total Minutes" TextBox to an int, then convert this int to a TimeSpan and then you can access the Hours and Minutes properties of the TimeSpan object to get the hours and minutes:

            private void btnProcess_Click(object sender, EventArgs e)
            {
                int totalMinutes = Convert.ToInt32(txtBxTotalMin.Text);
                TimeSpan ts = TimeSpan.FromMinutes(totalMinutes);
                txtBxTotal.Text = string.Format("{0} : {1}", ts.Hours, ts.Minutes);
            }
    Please remember to mark any helpful posts as answer and/or helpful.
    • Proposed as answer by Pantelis44999 Sunday, August 3, 2014 11:21 PM
    • Marked as answer by ynot333 Monday, August 4, 2014 2:30 AM
    Sunday, August 3, 2014 8:55 PM
  • Thank you the code worked really well, you were very helpful. :)

    ynot333

    Monday, August 4, 2014 2:32 AM
  • What if my totalMinutes is a value that goes greater than a day or suppose it's 1440 then the below properties would be "0":

    ts.Hours, ts.Minutes

    and the result would be : "0:0"

    Which is not exxpected. Could you provide a better solution?


    Thursday, November 28, 2019 10:16 AM
  • Hi Ashkay,

    This was a 5 year old post, and it's usually better to post your question in a new thread. But, I'll answer you here.

    The TimeSpan class also has a Days property, so use that in addition to the Hours and Minutes.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Thursday, November 28, 2019 4:17 PM