-
Aug 7th, 2020, 01:29 PM
#1
Thread Starter
Member
-
Aug 7th, 2020, 04:31 PM
#2
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
-
Aug 7th, 2020, 08:47 PM
#3
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 7th, 2020, 11:14 PM
#4
Re: How to convert total minutes to time format?
Originally Posted by dday9
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:
Dim minutes = 1470
Dim time = TimeSpan.FromMinutes(minutes)
Dim text = time.ToString("d\d\-hh\:mm")
If you wanted to use string interpolation:
vb.net Code:
Label1.Text = $"Answer is: {time:d\d\-hh\:mm}"
-
Aug 8th, 2020, 12:15 AM
#5
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 8th, 2020, 04:55 AM
#6
Thread Starter
Member
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.
-
Aug 8th, 2020, 10:08 AM
#7
Re: How to convert total minutes to time format?
Originally Posted by fa2020
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|