I am looking for functions that will verify the validity of a date or time in a string. Something like:
Function IsDate (DateString as string) as long
Function IsTime (TimeString as string) as long
You wold pass a string to it and it returns %TRUE only if the string contains a valid date or a valid time.
Does anyone have functions like this?
Thanks,
Robert
I did those one day for my friend Douglas, it envolved a Windows API call, but i forgot.
Basically i tried to use the data for something, if the api call failed, the date was not valid.
Try SystemTimeToFileTime. If the conversion succeeds, the date is valid...
About the time... there must be a similar way.
:)
Thanks for the info. I wound up writing my own routines in PB. When I have a little time, I will clean it up, document it, and post it.
Robert
It just so happened that today I needed an IsValidDate function for my program. :)
I took Elias' advice and drafted a little function and it works perfectly. It handles leap years and everything.
Here it is:
#Compile Exe
#Include "win32api.inc"
Function IsValidDate( ByVal sDate As String ) As Long
' sDate enters as YYYYMMDD
Local tSystemTime As SYSTEMTIME
Local tFileTime As FILETIME
' Split the date into it's components
tSystemTime.wYear = Val(Left$(sDate, 4))
tSystemTime.wMonth = Val(Mid$(sDate, 5, 2))
tSystemTime.wDay = Val(Right$(sDate, 2))
Function = SystemTimeToFileTime( tSystemTime, tFileTime )
End Function
Function PBMain() As Long
If IsValidDate("20080229") Then ' this was a leap year
? "good date"
Else
? "bad date"
End If
End Function
I thought there was something that could take looser input. Like "July 30, 2010" or "Jan 1, 1934" or "1989, Mar 23". Like Excel which tries to see a date whenever it can.
On a side note, while IsValidDate is a useful function, sometimes a IsRediculusDate function should also be used.
In processing voter registration files for political candidates, I've seen registration dates as old as 1801. I didn't check to see if they were actually valid.
If IsValidDate and IsRediculusDate both return True then you don't realy have a valid date.
I have extended Paul's example a little so that it can validate date, time, and a combination of both. In case you find it useful you can check it out in the Code example section:
http://www.planetsquires.com/protect/forum/index.php?topic=2630.0 (http://www.planetsquires.com/protect/forum/index.php?topic=2630.0)