Solved Time Calculation Help

Posted on 2007-10-01
Last Modified: 2008-01-09
Good Afternoon


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.

Question by:Jimbo99999
    LVL 11

    Expert Comment

    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))

    Author Comment


    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,

    LVL 48

    Expert Comment

    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)
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    "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.


    Author Comment


    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
    LVL 85

    Accepted Solution

    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"))
    LVL 11

    Assisted Solution

         Dim strT As String = "1010"
            Dim endT As String = "0910"
            Dim xDiff As String = calcDiff(strT, endT)
        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

    Author Comment

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

    Have a good day,

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now