Thursday, April 2, 2009

ReturnDate function in QTP

'This function returns the date in the specified format
'General usage returndate(offset,format)
'Examples: returndate(NULL,NULL) displays output in 4/1/2009 format
' returndate(2,NULL) displays output in 4/3/2009 format
' returndate(2,"mm-dd-yyyy") displays output in 04-03-2009 format
' returndate(2,"dd-mm-yy") displays output in 03-04-09 format
' returndate(2,"dd/mm/yy") displays output in 03/04/09 format

retdate=returndate(2,"dd-mm-yy")
msgbox retdate

public Function returndate(intOffset,strDateFormat)
Dim retDate
If isnull(intOffset)=-1 or isnull(strDateFormat)=-1 Then
retDate=date
End If
If isnull(intOffset)<>-1 Then
retDate=CDate(Date+intOffset)
End If
If isnull(strDateFormat)<>-1 Then
Select Case ucase(strDateFormat)
Case "MM-DD-YYYY"
retDate=Cstr(Right("0"+Cstr(month(retDate)),2)) +"-" + Cstr(Right("0" & CStr(Day(retDate)), 2))+"-" + Right(CStr(Year(retDate)), 4)
Case "MM/DD/YYYY" retDate=Cstr(Right("0"+Cstr(month(retDate)),2)) +"/" + Cstr(Right("0" & CStr(Day(retDate)), 2))+"/" + Right(CStr(Year(retDate)), 4)
Case "MM-DD-YY" retDate=Cstr(Right("0"+Cstr(month(retDate)),2)) +"-" + Cstr(Right("0" & CStr(Day(retDate)), 2))+"-" + Right(CStr(Year(retDate)), 2)
Case "MM/DD/YY" retDate=Cstr(Right("0"+Cstr(month(retDate)),2)) +"/" + Cstr(Right("0" & CStr(Day(retDate)), 2))+"/" + Right(CStr(Year(retDate)), 2)
Case "DD-MM-YYYY" retDate=Cstr(Right("0" & CStr(Day(retDate)), 2))+"-" +Cstr(Right("0"+Cstr(month(retDate)),2)) +"-" + Right(CStr(Year(retDate)), 4)
Case "DD/MM/YYYY" retDate=Cstr(Right("0" & CStr(Day(retDate)), 2))+"/" +Cstr(Right("0"+Cstr(month(retDate)),2)) +"/" + Right(CStr(Year(retDate)), 4)
Case "DD-MM-YY" retDate=Cstr(Right("0" & CStr(Day(retDate)), 2))+"-" +Cstr(Right("0"+Cstr(month(retDate)),2)) +"-" + Right(CStr(Year(retDate)), 2)
Case "DD/MM/YY" retDate=Cstr(Right("0" & CStr(Day(retDate)), 2))+"/" +Cstr(Right("0"+Cstr(month(retDate)),2)) +"/" + Right(CStr(Year(retDate)), 2)
End Select
End If
returndate=retDate
End Function

No comments: