Results 1 to 7 of 7

Thread: [RESOLVED] How to convert total minutes to time format?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2020
    Posts
    48

    Resolved [RESOLVED] How to convert total minutes to time format?

    Hi friends,
    I want to make a simple app which receives the total minutes through a text box as an input, and then after clicking the calculate button, the answer is written as a label object in the windows form. The format of the answer should be:
    dd-hh:mm
    For example: 1470 mins -------> 1d-00:30
    Please help me out.
    Attached Images Attached Images  
    Last edited by fa2020; Aug 7th, 2020 at 01:41 PM.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: How to convert total minutes to time format?

    The first thing you should be concerned with is your form validation. You're prompting for the total number of minutes, but you're not validating that what the user typed in the TextBox is a valid number. What you'll need to do is declare a variable with a numeric data type and then call TryParse from the respective data type against the TextBox value. If you only want to accept numbers without decimals then the variable's data type would be an Integer, but if you wanted to accept numbers with decimals then the data type would be a Double. So first thing's first, place this at the top of your Button's click event.
    Code:
    Dim minutes As Integer
    If (Not Integer.TryParse(TextBox1.Text, minutes)) Then
        MessageBox.Show("Please enter a valid number without decimals.", "Invalid Form", MessageBoxButtons.Ok)
        Return
    End If
    Once you've validated the form, you will want to create a new TimeSpan variable and specify the number of minutes. So after the conditional If/Then in the code above you'd add:
    Code:
    Dim timespanConversion As New TimeSpan(0, minutes, 0)
    Finally, you're wanting to display the TimeSpan in a custom format. This is a little tricky, but you'd need to call ToString on the TimeSpan variable and specify the format like such using String interpolation:
    Code:
    Label1.Text = $"Answer is: {timespanConversion.Days}d-{timespanConversion.Hours}:{timespanConversion.Minutes}"
    Here is an example fiddle for you to demonstrate the principles behind the answer: https://dotnetfiddle.net/L1jwr7
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How to convert total minutes to time format?

    As DDay says, you need to be sure you have valid input...

    https://docs.microsoft.com/en-us/dot...ew=netcore-3.1

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to convert total minutes to time format?

    Quote Originally Posted by dday9 View Post
    The first thing you should be concerned with is your form validation. You're prompting for the total number of minutes, but you're not validating that what the user typed in the TextBox is a valid number. What you'll need to do is declare a variable with a numeric data type and then call TryParse from the respective data type against the TextBox value. If you only want to accept numbers without decimals then the variable's data type would be an Integer, but if you wanted to accept numbers with decimals then the data type would be a Double. So first thing's first, place this at the top of your Button's click event.
    Code:
    Dim minutes As Integer
    If (Not Integer.TryParse(TextBox1.Text, minutes)) Then
        MessageBox.Show("Please enter a valid number without decimals.", "Invalid Form", MessageBoxButtons.Ok)
        Return
    End If
    Once you've validated the form, you will want to create a new TimeSpan variable and specify the number of minutes. So after the conditional If/Then in the code above you'd add:
    Code:
    Dim timespanConversion As New TimeSpan(0, minutes, 0)
    Finally, you're wanting to display the TimeSpan in a custom format. This is a little tricky, but you'd need to call ToString on the TimeSpan variable and specify the format like such using String interpolation:
    Code:
    Label1.Text = $"Answer is: {timespanConversion.Days}d-{timespanConversion.Hours}:{timespanConversion.Minutes}"
    Here is an example fiddle for you to demonstrate the principles behind the answer: https://dotnetfiddle.net/L1jwr7
    That's basically correct but there are some issues there.

    Firstly, I would suggest that you probably shouldn't be using a TextBox at all but, rather, a NumericUpDown, especially if you only want to accept whole numbers. The NumericUpDown won't display variable numbers of decimal places so that may be an issue in some cases, but not if you only want to work with Integers.

    Secondly, you should definitely call TimeSpan.FromMinutes rather than using that constructor. As the constructor will only accept Integer values, if you want to accept fractional minutes then FromMinutes, which accepts a Double, will be required anyway, unless you want to calculate the number of seconds yourself.

    Thirdly, assuming that you're using .NET 4.0 or later, you don't need such verbose formatting:
    vb.net Code:
    1. Dim minutes = 1470
    2. Dim time = TimeSpan.FromMinutes(minutes)
    3. Dim text = time.ToString("d\d\-hh\:mm")
    If you wanted to use string interpolation:
    vb.net Code:
    1. Label1.Text = $"Answer is: {time:d\d\-hh\:mm}"

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: How to convert total minutes to time format?

    TimeSpan.FromMinutes is suitable for your purposes. If you need to calulate a timespan from seconds, then use TimeSpan.FromSeconds

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2020
    Posts
    48

    Re: How to convert total minutes to time format?

    Ok, Thanks. It worked.
    How can I do some basic math operations in textbox?
    For example, I want to write 20+122+(3*45) in the text box. The text box should firstly calculate those numbers and then consider it as an input for further calculations.
    Is it possible?
    I need this feature in my work office. I need to add/multiply some numbers (minutes) and then convert the result into d-hh:mm
    By now, I do this in Excel, but I want to create a small app for doing this basic calculation, so I don't need to keep the whole excel app running in the background.
    Last edited by fa2020; Aug 8th, 2020 at 05:25 AM.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to convert total minutes to time format?

    Quote Originally Posted by fa2020 View Post
    Ok, Thanks. It worked.
    How can I do some basic math operations in textbox?
    For example, I want to write 20+122+(3*45) in the text box. The text box should firstly calculate those numbers and then consider it as an input for further calculations.
    Is it possible?
    I need this feature in my work office. I need to add/multiply some numbers (minutes) and then convert the result into d-hh:mm
    By now, I do this in Excel, but I want to create a small app for doing this basic calculation, so I don't need to keep the whole excel app running in the background.
    This has got nothing to do with the topic of this thread. One thread per topic and one topic per thread. The question you asked about here has been answered so use the Thread Tools menu to mark this thread Resolved. If you have a new question, create a new thread and provide all and only the information relevant to that question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width