• Status: Solved
• Priority: Medium
• Security: Public
• Views: 2328

# 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
0
Jimbo99999
• 3
• 2
• 2
• +1
2 Solutions

Commented:
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))
0

Author Commented:
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

0

IT Pro/DeveloperCommented:
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)
0

Middle School Assistant TeacherCommented:
"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.

0

Author Commented:
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
0

Middle School Assistant TeacherCommented:
You could do...

Dim startTime As String = "330"
Dim stopTime As String = "1224"

Dim dtA As DateTime = DateTime.ParseExact(startTime, "HHmm", Nothing)
Dim dtB As DateTime = DateTime.ParseExact(stopTime, "HHmm", Nothing)
Dim ts As TimeSpan = dtB.Subtract(dtA)

Debug.Print("Time: " & ts.Hours & ":" & ts.Minutes.ToString("00"))
0

Commented:
Dim strT As String = "1010"
Dim endT As String = "0910"
Dim xDiff As String = calcDiff(strT, endT)
MsgBox(xDiff)
Private Function calcDiff(ByVal s As String, ByVal e As String) As String
Dim retVal As String = ""

Dim sHR As Integer = CType(Mid(s, 1, 2), Integer)
Dim sMN As Integer = CType(Mid(s, 3, 2), Integer)

Dim eHR As Integer = CType(Mid(e, 1, 2), Integer)
Dim eMN As Integer = CType(Mid(e, 3, 2), Integer)

Dim xHr As Integer = eHR - sHR
Dim xMN As Integer = eMN - sMN

If xHr < 0 Then xHr += 24
If xMN < 0 Then xHr -= 1 : xMN += 60 : If xHr < 0 Then xHr += 24

retVal = Microsoft.VisualBasic.Strings.Right("00" & CType(xHr, String), 2)
retVal &= Microsoft.VisualBasic.Strings.Right("00" & CType(xMN, String), 2)

'eg strT="10:10", endT="10:10"
'   if you want this difference to calc = 2400 unrem the line bellow
'//If retVal = "0000" Then retVal = "2400"
Return retVal
End Function
0

Author Commented:
Thanks for the help Idle_Mind and AkisC...I will split the points.

Have a good day,
jimbo99999
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.