Tuesday, October 5, 2010

This VBScript function will give you the current date in the required format

Usage Examples:

MsgBox getDatestamp("MM/DD/YYYY")
' Returns 10/05/2010

MsgBox getDatestamp("DD-MM-YY")
' Returns 05-10-10

MsgBox getDatestamp("DD.MM.YYYY")
' Returns 05.10.2010

MsgBox getDatestamp("DD")
' Returns 05


------------------------------------------------------------------------------------------

Public Function getDatestamp(inFormat)
'********************************************
' Author : Chinmay Mudholkar
' Purpose : Getting the current date stamp
' Inputs : inFormat: The format that the date is required in. (eg: DD-MM-YY)
' Returns : The current date in the format required.
'********************************************

Dim retval, dd
retval = UCase(inFormat)

dd = Right("00" & Day(Date), 2)
mm = Right("00" & Month(Date), 2)

If InStr(retval, "DD") Then
retval = Replace(retval, "DD", dd)
End If

If InStr(retval, "MM") Then
retval = Replace(retval, "MM", mm)
End If

While InStr(retval, "YY")
If InStr(retval, "YYYY") Then
retval = Replace(retval, "YYYY", Year(Date))
ElseIf InStr(inFormat, "YY") Then
retval = Replace(retval, "YY", Right(Year(Date), 2))
End If
Wend

getDatestamp = retval
End Function

------------------------------------------------------------------------------------------

No comments:

Post a Comment