You are on page 1of 5

8/31/2016

VisualStudio

Home

.NET

DateAndTimeValidationFunctionsVisualStudiosamplecodedeveloperFusion

News Tutorials Code Training UserGroups Books Podcasts Forum Jobs

VisualStudio

Code

DateAndTimeValidationFunctions

Joinus

Signin

Indonesia

Thegenerationofrandomnumbersistooimportanttobelefttochance.RobertR.Coveyou

ByHariK,publishedon08Mar2006|Filedin VisualStudio VB.NET

0Comments

DateandTimeValidateFuntion
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

http://www.developerfusion.com/code/5480/dateandtimevalidationfunctions/

1/5

8/31/2016

DateAndTimeValidationFunctionsVisualStudiosamplecodedeveloperFusion

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")

http://www.developerfusion.com/code/5480/dateandtimevalidationfunctions/

2/5

8/31/2016

DateAndTimeValidationFunctionsVisualStudiosamplecodedeveloperFusion

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(

http://www.developerfusion.com/code/5480/dateandtimevalidationfunctions/

3/5

8/31/2016

DateAndTimeValidationFunctionsVisualStudiosamplecodedeveloperFusion

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

http://www.developerfusion.com/code/5480/dateandtimevalidationfunctions/

4/5

8/31/2016

DateAndTimeValidationFunctionsVisualStudiosamplecodedeveloperFusion

Return
False
End
If
If
((strMinute < "0")
Or
(strMinute > "59"))
Then
Return
False
End
If
Return
True
End
Function

Youmightalsolike...
Proj.Net
OracleJDeveloper11gHandbook:AGuidetoFusionWebDevelopment(OsborneORACLEPressSeries
JawsForWindowsvs2010AddIn
TheOfficialCharlotteSEO&SearchEngineMarketingSeptemberMeetup

Contribute

WebDevelopment

DeveloperJobs

Ourtools

Whynotwriteforus?Oryoucould
submitaneventorausergroupin
yourarea.Alternativelyjusttellus
whatyouthink!

ASP.NETQuickstart
Programmingnews
Javaprogramming
ASP.NETtutorials
C#programming

ASP.NETJobs
JavaJobs
DeveloperJobs

We'vegotautomaticconversion
toolstoconvertC#toVB.NET,
VB.NETtoC#.Alsoyoucan
compressjavascriptandcompress
cssandgeneratesqlconnection
strings.

19992016DeveloperFusionLtd

http://www.developerfusion.com/code/5480/dateandtimevalidationfunctions/

ManagedhostingbyEverycity

5/5

You might also like