Library code snippets
Date And Time Validation Functions
By Hari K, published on 08 Mar 2006
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
Related articles
Related discussion
-
Web App Service Project - Assistance Requested
by JusticeV (0 replies)
-
saving items in listboxes
by cryancaire (3 replies)
-
saving items in listboxes
by ramsenthil2000 (1 replies)
-
Buy cheap Xanax overnight. Cheap Xanax. Overnight delivery of Xanax in US no prescription needed. Cheapest Xanax.
by asleymar (0 replies)
-
Buy Soma online without a prescription. Soma drug no prescription. How to get Soma prescription. Soma cod accepted.
by asleymar (0 replies)
Related podcasts
-
Interview with Shawn Burke on Microsoft's .NET Source Code Release
Scott and Carl talk with Shawn Burke on the culmination of his many-year-old plan to get parts of the source of the .NET Framework released. With Visual Studio 2008, a simple process will allow developers to STEP INTO the .NET Framework Source from the IDE. This'll be a great debugging and learni...
Events coming up
-
Feb
25
Microsoft and Black Marble present Visual Studio 2010 and Managing the Application Lifecycle with TFS
Edinburgh, United Kingdom
Welcome to the Microsoft/Black Marble exploration of developer tools and application lifecycle management, now taking place in Edinburgh. Come along and find out how Microsoft's Developer Tools are changing as well as how Team Foundation Server, Best Practice and Testing can be implemented to help your business manage its application lifecycle successfully.
This thread is for discussions of Date And Time Validation Functions.