Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IsTime()

Status
Not open for further replies.

Robse

Programmer
Apr 28, 2003
28
DE
Hi I was wondering if there could be any issues with
this function I wrote, to determine whether a String is
a Time:

Public Function IsTime(sCheckTime As String) As Boolean
'
' Return true if the String passed to the
' function is a time (like 13:45 or 9:00 PM)
'
If DatePart("h", CDate(sCheckTime)) = 0 And _
DatePart("n", CDate(sCheckTime)) = 0 And _
DatePart("s", CDate(sCheckTime)) = 0 Then
IsTime = False
Else
IsTime = True
End If

End Function

It seems to work fine, but I want to make sure... if
there's a problems with it or you know a better way please tell me.

Thanks

BTW sCheckTime will always be convertible into a valid CDate,
so really the question should be "whether a CDate is a time"
 
"whether a cDate is a time": anything successfully converted to a date by CDate will contain a legitimate time component. And 0:0:0 is midnight...
 
Damn I forgot about midnight.
So what if I modify it like this:

If Not IsDate(sCheckTime) Then
IsTime = False
Exit Function
End IF

If CDate(sCheckTime) = CDate("00:00:00") Then
IsTime = True
Exit Function
End If

If DatePart("h", CDate(sCheckTime)) = 0 And _
DatePart("n", CDate(sCheckTime)) = 0 And _
DatePart("s", CDate(sCheckTime)) = 0 Then
IsTime = False
Else
IsTime = True
End If

Will that be OK?
Or is there an easier way and I'm doing
this completely the wrong way?

 
I have a text file that contains several "time" fields, i am trying to use SQL DTS to import theses fields into a SQL table, my problem is that i need to be able to check that the value stored in the text fields could be a valid time i.e 23:59 is valid, 56:17 isn't valid, :15 isn't valid
where a value isn't a valid time i need to return null and where it is, obviously i need the time. Anyone got any idea on how i could achieve this thru' VB

Cheers, Craig
Si fractum non sit, noli id reficere
 

This will do it...


Private Function isTime(s As String) As Boolean
On Error GoTo nottime
Dim d As Date
d = CDate("01 Jan 1900 " & s) 'error if not a valid time
isTime = True

Exit Function
nottime:
isTime = False
End Function




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top