Public Function func_convertDateFormat(ByVal DateString, ByVal SourceFormat, ByVal ReqFormat)
Dim strMonthVal ' to store the month value
Dim strDayVal ' to store the day value
Dim strYearVal ' to store the year value
Dim aTempVal ' to store the splitting date values in an array
If Not IsEmpty(DateString) Then
' select the specified source format
Select Case SourceFormat
Case "MM-DD-YYYY"
' split and store the month,day and year values in variables
aTempVal = Split(DateString, "-")
'verify input date string whether it is in source format or not.
If Ubound(aTempVal) = 2 Then
strMonthVal = aTempVal (0)
strDayVal = aTempVal (1)
strYearVal = aTempVal (2)
Else
func_convertDateFormat = "ErrMsg: Input parameter DateString is not in specified source format"
End If
Case "YYYY-MM-DD"
' split and store the month,day and year values in variables
aTempVal = Split(DateString, "-")
'verify input date string whether it is in source format or not.
If Ubound(aTempVal) = 2 Then
strYearVal = aTempVal (0)
strMonthVal = aTempVal (1)
strDayVal = aTempVal (2)
Else
func_convertDateFormat = "ErrMsg: Input parameter DateString is not in specified source format"
End If
Case "MM/DD/YYYY"
' split and store the month,day and year values in variables
aTempVal = Split(DateString, "/")
'verify input date string whether it is in source format or not.
If Ubound(aTempVal) = 2 Then
strMonthVal = aTempVal (0)
strDayVal = aTempVal (1)
strYearVal = aTempVal (2)
Else
func_convertDateFormat = "ErrMsg: Input parameter DateString is not in specified source format"
End If
Case "YYYY/MM/DD"
' split and store the month,day and year values in variables
aTempVal = Split(DateString, "/")
'verify input date string whether it is in source format or not.
If Ubound(aTempVal) = 2 Then
strYearVal = aTempVal (0)
strMonthVal = aTempVal (1)
strDayVal = aTempVal (2)
Else
func_convertDateFormat = "ErrMsg: Input parameter DateString is not in specified source format"
End If
Case "DD/MM/YYYY"
' split and store the month,day and year values in variables
aTempVal = Split(DateString, "/")
'verify input date string whether it is in source format or not.
If Ubound(aTempVal) = 2 Then
strDayVal = aTempVal (0)
strMonthVal = aTempVal (1)
strYearVal = aTempVal (2)
Else
func_convertDateFormat = "ErrMsg: Input parameter DateString is not in specified source format"
End If
Case Else
'returns false when sorce format is not in the specified list.
func_convertDateFormat = "ErrMsg: Input parameter" & " " & SourceFormat & " " & "is not valid"
Exit Function
End Select
' select the specified source format
Select Case ReqFormat
' Combine the month,day and year values in variables to get required format
Case "MM-DD-YYYY"
func_convertDateFormat = strMonthVal & "-" & strDayVal & "-" & strYearVal
Case "YYYY-MM-DD"
func_convertDateFormat = strYearVal & "-" & strMonthVal & "-" & strDayVal
Case "MM/DD/YYYY"
func_convertDateFormat = strMonthVal & "/" & strDayVal & "/" & strYearVal
Case "DD/MM/YYYY"
func_convertDateFormat = strDayVal & "/" & strMonthVal & "/" & strYearVal
Case "YYYY/MM/DD"
func_convertDateFormat = strYearVal & "/" & strMonthVal & "/" & strDayVal
Case Else
func_convertDateFormat = "ErrMsg: Input parameter" & " " & ReqFormat & " " & "is not valid"
Exit Function
End Select
Else
func_convertDateFormat = "ErrMsg: Input parameter" & " " & DateString & " " & "is not valid"
End If
End Function