Date And Time Validation Functions

Date and Time Validate Funtion

Public
Enum
OptFormat
DDMMYYYY = 1
DDMMMYYYY = 2
End
Enum
Public
Function
 gfncIsDateValid(
ByVal
 strDate
As
String
,
ByVal
 strValidFormat
As
 OptFormat,
Optional
ByVal
 strDateOptional
As
Boolean
 =
True
)
As
Boolean
'**********************************************************************************
'DESCRIPTION : This function is used to check if the Date passed is valid
' or not. It returns a Boolean Value. Two Type Format can be
' mentioned here. Format Type DDMMYYYY and Type DDMMMYYYY are
' available.
'INPUT PARAMETER : Date should be mentioned as string. Format should also be
  ' mentioned. True or False should also be passed. ' True should be mentioned
  if Date can be empty. ' False should be mentioned if Date should not be empty.
'RETURN VALUE : Returns Boolean True or False [True- If Date is in Valid
' Format] [False- If the Date is in InValid Format]
'HOW TO USE [Ex] : gfncIsDateValid(TextBox1.Text, modGeneralFnc.OptFormat.DDMMMYYYY,)
'**********************************************************************************
Dim
 strGetDate
As
String
Dim
 strGetDay
As
String
, strGetMonth
As
String
, strGetYear
As
String
Dim
 strDaysInMonth
As
String
  strGetDate = Trim(strDate)  
If
 strGetDate ="" Then
If
 strDateOptional =
True
Then
Return
True
Else
Return
False
End
If
End
If
Select
Case
 strValidFormat
Case
 OptFormat.DDMMYYYY
If
Not
 Len(Trim(strGetDate)) = 10
Then
Return
False
End
If
If
 InStr(strGetDate, pstrDateDelimiter, CompareMethod.Binary) = 0
Then
Return
False
End
If
If
 ((strGetDate.Substring(2, 1) <> pstrDateDelimiter)
  Or
   (strGetDate.Substring(5, 1) <> pstrDateDelimiter))
  Then
  Return
  False
  End
  If
    strGetDay = strGetDate.Substring(0, 2)strGetMonth = strGetDate.Substring(3,
  2)strGetYear = strGetDate.Substring(6, 4)  
  If
  Not
   ((IsNumeric(strGetDay))
  And
   (IsNumeric(strGetMonth))
  And
   (IsNumeric(strGetYear)))
  Then
  Return
  False
  End
  If
  If
   ((strGetDay < "0")
  Or
   (strGetMonth < "0")
  Or
   (strGetYear < "0"))
  Then
  Return
  False
  End
  If
  If
   ((strGetMonth = "01")
  Or
   (strGetMonth = "03")
  Or
   (strGetMonth = "05")
  Or
   (strGetMonth = "07")
  Or
   (strGetMonth = "08")
  Or
   (strGetMonth = "10")
  Or
   (strGetMonth = "12"))
  Then
    strDaysInMonth = "31" ElseIf
 ((strGetMonth = "04")
  Or
   (strGetMonth = "06")
  Or
   (strGetMonth = "09")
  Or
   (strGetMonth = "11"))
  Then
    strDaysInMonth = "30" ElseIf
 (strGetMonth = "02")
  Then
  If
   (
  CInt
  (strGetYear)
  Mod
   4) = 0
  Then
  ''Leap year
    strDaysInMonth = "29" Else
  strDaysInMonth = "28" End
If
Else
Return
False
End
If
If
 (strGetDay > strDaysInMonth)
  Then
  Return
  False
  Else
  Return
  True
  End
  If
  Case
   OptFormat.DDMMMYYYY
  If
  Not
   Len(Trim(strGetDate)) = 11
  Then
  Return
  False
  End
  If
  If
   InStr(strGetDate, pstrDateDelimiter, CompareMethod.Binary) = 0
  Then
  Return
  False
  End
  If
  If
   ((strGetDate.Substring(2, 1) <> pstrDateDelimiter)
  Or
   (strGetDate.Substring(6, 1) <> pstrDateDelimiter))
  Then
  Return
  False
  End
  If
    strGetDay = strGetDate.Substring(0, 2)strGetMonth = strGetDate.Substring(3,
  3).ToUpper()strGetYear = strGetDate.Substring(7, 4)  
  If
  Not
   ((IsNumeric(strGetDay))
  And
   (IsNumeric(strGetYear)))
  Then
  Return
  False
  End
  If
  If
   ((strGetDay < "0")
  Or
   (strGetYear < "0"))
  Then
  Return
  False
  End
  If
  If
   ((strGetMonth = "JAN")
  Or
   (strGetMonth = "MAR")
  Or
   (strGetMonth = "MAY")
  Or
   (strGetMonth = "JUL")
  Or
   (strGetMonth = "AUG")
  Or
   (strGetMonth = "OCT")
  Or
   (strGetMonth = "DEC"))
  Then
    strDaysInMonth = "31" ElseIf
 ((strGetMonth = "APR")
  Or
   (strGetMonth = "JUN")
  Or
   (strGetMonth = "SEP")
  Or
   (strGetMonth = "NOV"))
  Then
    strDaysInMonth = "30" ElseIf
 (strGetMonth = "FEB")
  Then
  If
   (
  CInt
  (strGetYear)
  Mod
   4) = 0
  Then
  ''Leap year
    strDaysInMonth = "29" Else
  strDaysInMonth = "28" End
If
Else
Return
False
End
If
If
 (strGetDay > strDaysInMonth)
  Then
  Return
  False
  Else
  Return
  True
  End
  If
  End
  Select
  End
  Function
 
        Public  
    Function
     gfncFormatedDate(
    ByVal
     strDate
    As  
    String
    ,
    ByVal
     strValidFormat
    As
     OptFormat,
    Optional  
    ByVal
     strDateOptional
    As  
    Boolean
     =
    True
    )
    As  
    String  
    '**********************************************************************************
   'DESCRIPTION : This function is used to check if the Date passed is valid
   ' or not. It returns a Boolean Value. Two Type Format can be ' mentioned here.
   Format Type DDMMYYYY and Type DDMMMYYYY are ' available.
    'INPUT PARAMETER : Date should be mentioned as string. Format should also
   be ' mentioned. True or False should also be passed.
    ' True should be mentioned if Date can be empty.  
    ' False should be mentioned if Date should not be empty.  
    'RETURN VALUE : Returns String   
    'HOW TO USE [Ex] : gfncIsDateValid(TextBox1.Text, modGeneralFnc.OptFormat.DDMMMYYYY)
'**********************************************************************************
   Dim strGetDate
    As  
    String  
    Dim
     strGetDay
    As  
    String
    , strGetMonth
    As  
    String
    , strGetYear
    As  
    String  
    Dim
     strDaysInMonth
    As  
    String
        strGetDate = Trim(strDate)    
    Select  
    Case
     strValidFormat
    Case
     OptFormat.DDMMYYYYstrGetDay = strGetDate.Substring(0, 2)strGetMonth
    = strGetDate.Substring(3, 2)strGetYear = strGetDate.Substring(6, 4)
    Return
     strGetDay & "/" & gfncGetMonth(strGetMonth) & "/" & strGetYear
    Case
     OptFormat.DDMMMYYYYstrGetDay = strGetDate.Substring(0, 2)strGetMonth
    = strGetDate.Substring(3, 3).ToUpper()strGetYear = strGetDate.Substring(7,
    4)
    Return
     strGetDay & "/" & strGetMonth & "/" & strGetYear
    End  
    Select  
    End  
    Function
    
            Private    
      Function
       gfncGetMonth(
      ByVal
       strMonth
      As    
      String
      )
      As    
      String    
      Select    
      Case
       strMonth
      Case"01"
  Return"Jan"
  Case"02"
  Return"Feb"
  Case"03"
  Return"Mar"
  Case"04"
  Return"Apr"
  Case"05"
  Return"May"
  Case"06"
  Return"Jun"
  Case"07"
  Return"Jul"
  Case"08"
  Return"Aug"
  Case"09"
  Return"Sep"
  Case"10"
  Return"Oct"
  Case"11"
  Return"Nov"
  Case"12"
  Return"Dev"
  End    
  Select    
  End    
  Function
      
            Public      
    Function
     gfncIsTimeValid(
    ByVal
     strTime
    As      
    String
    ,
    Optional      
    ByVal
     strTimeOptional
    As      
    Boolean
     =
    True
    )
    As      
    Boolean      
    '**********************************************************************************
  'DESCRIPTION : This function is used to check if the time passed is a valid
  ' time or not. It returns a Boolean Value. 'INPUT PARAMETER : Time is passed
  and True or False should be passed as 2nd param. ' True should be mentioned
  if time can be empty. ' False should be mentioned if time should not be
  empty. 'RETURN VALUE : Returns Boolean True or False [True- If Time is
  in Valid ' Format] [False- If the Time is in InValid Format]
    'HOW TO USE [Ex] : gfncIsTimeValid(Variable)      
    '**********************************************************************************
  Dim strHour
    As      
    String      
    Dim
     strMinute
    As      
    String
            strTime = Replace(Trim(strTime), " ", "")
        If
         strTime ="" Then      
If
 strTimeOptional =
True      
Then      
Return      
True      
Else      
Return      
False      
End      
If      
End      
If      
If
 (Len(strTime) <> 5)
        Then      
        Return      
        False      
        End      
        If      
        If
         (strTime.Substring(2, 1) <> pstrTimeDelimiter)
        Then      
        Return      
        False      
        End      
        If
                strHour = strTime.Substring(0, 2)strMinute = strTime.Substring(3,
        2)        
        If      
        Not
         ((IsNumeric(strHour))
        And
         (IsNumeric(strMinute)))
        Then      
        Return      
        False      
        End      
        If      
        If
         ((strHour < "0")
        Or
         (strMinute < "0"))
        Then      
        Return      
        False      
        End      
        If      
        If
         ((strHour < "0")
        Or
         (strHour < "23"))
        Then      
        Return      
        False      
        End      
        If      
        If
         ((strMinute < "0")
        Or
         (strMinute > "59"))
        Then      
        Return      
        False      
        End      
        If      
        Return      
        True      
        End      
        Function

You might also like...

Comments

Hari K I am Hari, TL in a Pvt Firm. I have knowledge in Dot Net, WM, VB6, Visual Foxpro, Foxpro, SQL Server, Oracle, Access, DBase and Crystal Report.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“It works on my machine.” - Anonymous