[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB.net Time Calculation Help

Posted on 2007-10-01
8
Medium Priority
?
2,315 Views
Last Modified: 2008-01-09
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
Comment
Question by:Jimbo99999
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 11

Expert Comment

by:AkisC
ID: 19993446
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 Comment

by:Jimbo99999
ID: 19993550
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
 
LVL 48

Expert Comment

by:jpaulino
ID: 19993604
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19994733
"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 Comment

by:Jimbo99999
ID: 19995943
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 19996159
You could do...

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

        startTime = startTime.PadLeft(4, "0")
        stopTime = stopTime.PadLeft(4, "0")
        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
 
LVL 11

Assisted Solution

by:AkisC
AkisC earned 1000 total points
ID: 19996412
     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 Comment

by:Jimbo99999
ID: 19999740
Thanks for the help Idle_Mind and AkisC...I will split the points.  

Have a good day,
jimbo99999
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Loops Section Overview
Suggested Courses
Course of the Month18 days, 10 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question