|
-
Jan 17th, 2008, 09:55 PM
#1
Thread Starter
Member
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!
-
Jan 17th, 2008, 10:17 PM
#2
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
-
Jan 17th, 2008, 10:24 PM
#3
Thread Starter
Member
Re: 12 Hour functions
Ah thanks so much!
I never knew about the datediff function.
-
Jan 17th, 2008, 10:36 PM
#4
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
-
Jan 17th, 2008, 11:41 PM
#5
Re: 12 Hour functions
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|