Link to home
Start Free TrialLog in
Avatar of Jimbo99999
Jimbo99999Flag for United States of America

asked on

VB.net Time Calculation Help

Good Afternoon

Experts:

I have a bit of an unusual situation here trying to calculate time between 2 values.  Two fields are present(start and end time) are in HHMM format.  So, the hours can be between 00 and 24 and the minutes between 00 and 59.  Potnetially an example would be 0330 and 2424.  

Perhaps I need to do some formatting before that calculation, which I don't mind doing, but I am unsure how to approach this.  I appreciate any ideas.

Thanks,
jimbo99999
Avatar of AkisC
AkisC
Flag of Greece image

What exactly do you want to calculate
If the values are in a Textbox then
        Dim sStr As String = Me.TextBox1.Text
'// so lets say that the value of your Textbox is
        sStr = "0330"
        Dim HR As String = Mid(sStr, 1, 2)
        Dim MN As String = Mid(sStr, 2, 2)
        Dim xSec As Long = 0
        If IsNumeric(HR) Then
            If CType(HR, Integer) > 0 And CType(HR, Integer) < 25 Then
                xSec = CType(HR, Integer) * 60
            End If
        End If
        If IsNumeric(MN) Then
            If CType(MN, Integer) > 0 And CType(MN, Integer) < 60 Then
                xSec += CType(MN, Integer)
            End If
        End If
        MsgBox("Total Minutes for [" & sStr & "] are: " & CType(xSec, String))
Avatar of Jimbo99999

ASKER

AkisC:

Thanks for your reply.  My 0330 example is in HHMM format.  I have all the validation done to make sure the right 2 positions are greater than 00 and less than 60 for the minutes.  Then I check the first 2 or the first 1(depending on the value) to make sure it is greater than 00 and less than 24.  I also have the validation for the Numerics done also.  

So, now I just need the length of time between the 2 values.  0330(3:30) to 2424(12:24) would be how many hours and minutes?  This is what I need to get.

Thanks for the help,
jimbo99999





If you already have the times in the right format (3:30  and  12:24) the you can use datediff

' for the hours
DateDiff(DateInterval.Hour, Time2, Time1)

' for the minutes
DateDiff(DateInterval.Minute, Time2, Time1)
"So, the hours can be between 00 and 24"

Normally hours are between 0 and 23:
    12:24 PM (24 minutes after noon) would be 1224.
    12:24 AM (24 minutes after midnight) would be 0024.

Idle_Mind:

We are utilizing military time reference so as to avoid needing to specify an additional field for AM or PM.
Also, there will be no use of the colon between the hours and the minutes.  As a result, my format is HHMM.  So, I will use hours between 00 and 24...including 00 but not including 24.  
The User will enter 130 for 1:30am and 13:30 for 1:30pm and so forth.  A starting and ending time will be entered and I must calculate the time difference between the 2.  

Civilian Time Military Time
12:00 AM 0000 hrs
1:00 AM 0100 hrs
2:00 AM 0200 hrs
3:00 AM 0300 hrs
4:00 AM 0400 hrs
5:00 AM 0500 hrs
6:00 AM 0600 hrs
7:00 AM 0700 hrs
8:00 AM 0800 hrs
9:00 AM  0900 hrs
10:00 AM 1000 hrs
11:00 AM 1100 hrs
12:00 PM 1200 hrs
1:00 PM 1300 hrs
2:00 PM 1400 hrs
3:00 PM 1500 hrs
4:00 PM 1600 hrs
5:00 PM 1700 hrs
6:00 PM 1800 hrs
7:00 PM 1900 hrs
8:00 PM 2000 hrs
9:00 PM 2100 hrs
10:00 PM 2200 hrs
11:00 PM 2300 hrs
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help Idle_Mind and AkisC...I will split the points.  

Have a good day,
jimbo99999