Page History: IsDate and IsTime functions (VB Work-A-Like)
    Compare Page Revisions
 
    
    
    
    
    
    Page Revision: 2018/09/25 18:35
The following are two functions to validate dates and times. 
Inspired by the VB functions of the same name.
Often when a user is asked to enter either, there is quite a bit of code to make sure that what they entered makes sense. These functions provide a compact check on both the format and values and return a boolean (actually an integer) of 0  if the string is not a Date/Time. Non zero indicates the string is valid.
Notes IsDate:- Supports both UK/AUS time formats of dd/mm/yyyy or international sort format of yyyy/mm/dd
- If opt is zero or absent for IsDate, system default format is assumed, if opt is non-zero, international format is assumed.
- The delimiter may be either / or - e.g. dd-mm-yyyy or dd/mm/yyyy is fine.
 
Notes IsTime:- Times must be formatted as hh:mm (no seconds allowed but you could easily tweak it)
 
Syntax:=IsTime(TimeStr$)
=IsDate(DateStr$,[opt])
Examples:If Not IsDate(dt$,1) then exit Sub
Checkbox=Istime(A$)
Dependencies:IsLeapYear FunctionRegular Expression Function
 Function IsTime(t$) As Integer ' times must be hh:mm:ss
	IsTime=0
	If Match("^[012][0123456789]:[012345][0123456789]:[012345][0123456789]$",t$) Then
		If Val(Left$(t$,2))<24 Then
			If Val(Mid$(t$,4,2))<60 Then
				If Val(Right$(t$,2))<60 Then
					IsTime=1
				EndIf
			EndIf
		EndIf
	EndIf
 End Function
 
  Function IsDate(t$,opt As Integer) As Integer
	Local Integer d,m,y
	IsDate=0
	If opt=0 then
		If Match("^[0123][0123456789][/|-][01][0123456789][/|-][12][0123456789][0123456789][0123456789]$",t$)=0 Then Exit Function
		d=Val(Left$(t$,2))
		m=Val(Mid$(t$,4,2))
		y=Val(Right$(t$,4))
	Else
		If Match("^[12][0123456789][0123456789][0123456789][/|-][01][0123456789][/|-][0123][0123456789]$",t$)=0 Then Exit Function
		d=Val(Right$(t$,2))
		m=Val(Mid$(t$,6,2))
		y=Val(Left$(t$,4))
	EndIf
	Select Case m
		Case 1,3,5,7,8,10,12
			if d<1 or d>31 Then Exit Function
		Case 4,6,9,11
			if d<1 or d>30 Then Exit Function
		Case 2
			if d<1 or d>28+IsLeapYear(y) Then Exit Function
	End Select
	IsDate=1
 End Function