Answered Time and Math

RadBrad

Member
Joined
Nov 2, 2009
Messages
6
Programming Experience
Beginner
Hello All,

This is probably easy, but I'm struggling with it.

I want to make a very small program where the user inputs a starting time and an ending time that is in "clock time (?)" and the program converts it to total minutes in one box and a decimal total in a second box.

I have 4 textboxes, 2 are masked text boxes (military time) for input and two are not, for output and the math is coming out weird. If I put a start time of 0925 and an end time of 1015, the results I'm expecting would be 50 total minutes and .83333 total as a decimal. I don't think I'm converting the textbox input correctly as I'm not getting anything near that. I am using a button to calculate and another to clear the boxes, just FYI.

Also, I'm not sure what to do about any times that span the noon hour except to use an absolute value, so any thoughts with that would be great!

Thanks in advance!

VB.NET:
    Public Function Time()
        Dim StartTime As Double
        Dim Endtime As Double

        StartTime = txtStart.Text
        Endtime = txtEnd.Text

        txtDecimal.Text = (Endtime - StartTime) / 60
        txtMinutes.Text = Endtime - StartTime

        End Function
 
Last edited:
Couple of things, Functions are meant to return something this should be a sub.
Turn on Option Strict so you don't get any typecasting error when trying to convert type double to string.
You can use TimeSpan to parse the time then subtract them like:
VB.NET:
Dim t1 As TimeSpan = TimeSpan.Parse("09:25")
Dim t2 As TimeSpan = TimeSpan.Parse("10:15")
MessageBox.Show((t2 - t1).TotalHours.ToString("0.##"))
Which would be:
VB.NET:
Dim t1 As TimeSpan = TimeSpan.Parse(txtStart.Text)
Dim t2 As TimeSpan = TimeSpan.Parse(txtEnd.Text)
txtDecimal.Text = (t2 - t1).TotalHours.ToString("0.##")
 
Last edited:
I even tried it with military time and it works fine. TotalHours function returns a double I just capped the number of decimal spots with the .ToString("0.##") other wise it would be the full decimal equivalent. :cool:
 
It's not your code I don't have faith in... it's my programming ability. I've never heard of the Timespan method. Right now, I don't exactly understand what the parsing is doing. The numbers I get using 0925 as a start time and 1015 as a stop time are these: decimal = -369.829166666667; minutes = -22189.75. I am still expecting to have decimal = .8333 and minutes = 50.

Here is the modified code:

VB.NET:
    Public Sub Time()
        Dim StartTime As TimeSpan = TimeSpan.Parse(txtStart.Text)
        Dim EndTime As TimeSpan = TimeSpan.Parse(txtEnd.Text)
        Dim TotalTime As TimeSpan

        TotalTime = (EndTime - StartTime)

        txtDecimal.Text = TotalTime.TotalHours.ToString("0.##") / 60
        txtMinutes.Text = TotalTime.TotalHours.ToString("0.##")

    End Sub

Thank you for your help so far!
 
It worked just as you asked for. Why make a third object just to equal the diff b/t the other two? And you don't need to ( / 60), TotalHours will convert a time of less than 1 hr to a decimal. If you need the minutes use .TotalMinutes.
TimeSpan Class
 
Last edited:
I am sure that it is giving me what I asked for... so what I asked for is not what I want. I will try again to ask, but bear with me... I may not know what to ask for.

With this code:
VB.NET:
    Public Sub Time()
        Dim StartTime As TimeSpan = TimeSpan.Parse(txtStart.Text)
        Dim EndTime As TimeSpan = TimeSpan.Parse(txtEnd.Text)

        MessageBox.Show((EndTime - StartTime).TotalMinutes.ToString("0.##"))

    End Sub

Using a start time of 09:25 and an ending time of 10:15, I am returned a value of -1331385. Maybe this is what I asked for. When I use my fingers to count the minutes in between 09:25 and 10:15, I come up with 50. So the follow up would be:

A) What measurement is -1331385? Ticks? Nanoseconds?
B) Why is it negative? If the end time is higher than the start time, it should be positive...
C) How do I make -1331385 equate to 50 minutes of "real world time", which is what I need in the end?

Thanks again. I am not trying to make this difficult. I am just trying to understand.
 
I just retried the code with the .TotalMinutes and get "50".
If I use .TotalHours I get "0.83".
Your inputs need to be in a "HH:MM" format.
Double check some stuff.
 
Ok. I have the Masked text box set to that time mask. I assumed it made it that format. I will look into it tomorrow.

Thank you again.
 
The default for MaskedTextbox is IncludeLiterals in the CutCopyMaskFormat property which keeps the mask characters in the text property - so 09:25 = 09:25 otherwise it could look like 0925 to the TimeSpan. Which would give you a strange result.
 
Last edited:
Ok. I have the Masked text box set to that time mask. I assumed it made it that format.
The MaskedTextBox not only enforces the input mask, it can also validate the data type, and produce the value. When you select one of the Time formats you can see the validating type is DateTime (the Date data type in VB), there is a checkbox in this dialog for "Use validating type". This enable you to use the ValidateText function to return a Date object from the parsed input text. The difference between two Date values is a TimeSpan.
VB.NET:
Dim A As Date = CDate(Me.MaskedTextBox1.ValidateText)
Dim B As Date = CDate(Me.MaskedTextBox2.ValidateText)
Dim time As TimeSpan = B - A
CDate is still needed here with Option Strict, it's not for conversion but for casting the return type of ValidateText from Object to Date.
 
Back
Top