Thursday, January 8, 2015

Format Date Function


'###############################################################################
'Name: GetDate()
'Created[By] : msrivastava
'@Description Purpose: This function is used to format our date depending on the format code
'@Documentation Parameters: <inDate>  Date to be reformated
'<fmt> containing the formatting code for the date
'###############################################################################

Public Function GetDate (inDate, fmt)'(IntlDateCodes fmt)
    Dim outDate
Dim dtTime
Dim sDay, sMonth, sYear, sHour, sMinute

'Get our current date
inDate = DateValue(inDate)
'Break it out into month, day, and year
' For the month, day, minute, or hour we might end up with a single digit. In that case, check the length.
' If the length is one, append a 0 to make it two characters.
' Day
sDay = Day(inDate)
If Len(sDay) = 1 Then
sDay = "0" & sDay
End If
' Month
sMonth = Month(inDate)
If Len(sMonth) = 1 Then
sMonth = "0" & sMonth
End If
' Year
sYear = Year(inDate)
' Hour
sHour = Hour(Now)
If Len(sHour) < 2 Then
sHour = "0" & sHour
End If
' Minute
sMinute = Minute(Now)
If Len(sMinute) < 2 Then
sMinute = "0" & sMinute
End If
'Check the date format and stick the date pieces together with slashes in between, based on the language
Select Case fmt
Case "en-GB"
outDate = sDay & "/" & sMonth & "/" & sYear 'dd/mm/yyyy format

Case "en-GB_Short"
outDate = sDay & "/" & sMonth & "/" & right(sYear, 2) 'dd/mm/yy format

        Case "en-US"
outDate = sMonth& "/"& sDay& "/"& sYear  'mm/dd/yyyy format

Case "Archive"
outDate = sYear& "-"& sMonth& "-"& sDay  'yyyy-mm-dd format

Case "Archive_Strip"
outDate = sYear& sMonth& sDay  'yyyymmdd format

Case "Short"
            outDate = sMonth& "/"& sDay& "/"& right(sYear,  2)   'mm/dd/yy format

Case "Strip"
outDate = sMonth & sDay & sYear    'mmddyyyy format

Case "Short_Strip"
outDate = sMonth& sDay& right(sYear, 2)  'mmddyy format

Case "Strip-GB"
outDate = sDay & sMonth & sYear    'ddmmyyyy format

Case "Dmd Date-time"
outDate = sMonth & sDay & sHour & sMinute  'mmddhhmm  format

Case "-Year"
outDate = sMonth & sDay 'mmdd  format

Case "StripZeros"
outDate = Month(inDate)& "/"& Day(inDate)& "/"& sYear

Case "SysDate"
outDate = DateAdd("d", 0, inDate)   '#DATE#  format

Case "Underscore-US"
outDate = sMonth& "_"& sDay& "_"& sYear    'mm_dd_yyyy format

Case "DateTime-US"
dtTime = sHour& ":"& sMinute 'mm/dd/yyyy hh:mm
outDate = sMonth& "/"& sDay& "/"& sYear& " "& dtTime 'mm/dd/yyyy hh:mm

Case "DateTime-GB"
dtTime = sHour& ":"& sMinute 'dd/mm/yyyy hh:mm
outDate = sDay & "/" & sMonth & "/" & sYear& " "& dtTime 'dd/mm/yyyy hh:mm
    End Select

   GetDate = outDate
End Function

No comments: