Results 1 to 5 of 5

Thread: 12 Hour functions

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    38

    12 Hour functions

    I am trying to make a vb6 script that reads from a text file.
    An example of a line from the text file is:
    1/17/2008-8:53:25 PM-John

    Is there a way to check if this was 12 hours ago? As in calculate the time passed since 1/17/2008-8:53:25 PM-John.

    Thanks!

  2. #2
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: 12 Hour functions

    Cast the date and time from the string into a Date type and use the DateDiff function:
    Code:
    Private Sub TryThis()
    
        Dim sInput As String, dDate As Date
        
        sInput = "1/17/2008-8:53:25 PM-John."
        sInput = Left$(sInput, InStr(1, sInput, " "))
        sInput = Replace$(sInput, "-", " ")
        
        dDate = CDate(sInput)
        If DateDiff("h", dDate, Now) > 12 Then
            Debug.Print "More than 12 hours."
        End If
        
    End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    38

    Re: 12 Hour functions

    Ah thanks so much!
    I never knew about the datediff function.

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: 12 Hour functions

    You can find out a lot of the functions you never knew about if you open up the Object Browser by pressing F2

  5. #5
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: 12 Hour functions

    Quote Originally Posted by Comintern
    sInput = "1/17/2008-8:53:25 PM-John."
    sInput = Left$(sInput, InStr(1, sInput, " "))
    Two issues:

    1. With sInput = Left$(sInput, InStr(1, sInput, " "))
    that has chopped off the "PM" and the time becomes "AM".

    2. Depend on what you want, but DateDiff() with "h" doesn't care about minutes and seconds:
    DateDiff("h", "1/17/2008 8:59:59 PM", "1/17/2008 9:00:00 PM") gives you 1 hour even only one second passed.
    (similar with other intervals).

    This is my way:
    Code:
    Private Sub TryThis()
       Dim sInput  As String
       Dim dt      As Variant
       Dim Elapsed As Single
        
       sInput = "1/17/2008-8:53:25 PM-John."
        
       dt = Split(sInput, "-")
       dt = CDate(dt(0) & " " & dt(1))
       Elapsed = (Now - dt)
       If Elapsed > 0.5 Then '-- half a day
          MsgBox "Time passed: More than 12 hours."
       Else
          MsgBox "Time passed: " & Format(Elapsed, "hh:mm")
       End If
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width