Full Version: date formatting
UtterAccess Discussion Forums > Microsoft® Access > Access Queries
sergeanthatch
Does any one know how I can format a date that currently displays as 12/31/1975 as 31st December 1975?
Larry Larsen
Hi
Try format function:

? Format(#12/31/1975#,"dd mmmm yyyy")
31 December 1975

HTH's
thumbup.gif
sergeanthatch
got that part.... I want to cantonate the proper suffix to the day ie: 31 st 22 nd 13 th
orange999
Here's a function that should do it.

CODE
'---------------------------------------------------------------------------------------
' Procedure : dateOrd
' Author    : Jack
' Created   : 1/28/2010
' Purpose   : To format a date into day (ordiinal) full Month and 4 digit year.
'
' Example:   31/12/1975    gives 31st December 1975
'            15/10/2003    gives 15th October 2003
'---------------------------------------------------------------------------------------
' Last Modified:
'
' Inputs: A valid date
' Dependency: N/A
'------------------------------------------------------------------------------
'
Function dateOrd(d1 As Date) As String
Dim i As Integer
Dim ret As String
Dim s As String
   On Error GoTo dateOrd_Error

s = Format(d1, " MMMM YYYY")
i = Format(d1, "dd")

Select Case i
Case i = 1 Or 21 Or 31
sufx = "st"
Case i = 2 Or 22
sufx = "nd"
Case i = 3 Or 23
sufx = "rd"
Case Else
sufx = "th"

End Select
dateOrd = i & sufx & s
'Debug.Print ret

   On Error GoTo 0
   Exit Function

dateOrd_Error:

    MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure dateOrd"
End Function


HTH
sergeanthatch
you all are the best
Larry Larsen
Hi
Also anther one...:
CODE
Function formatday(dtDate) As String

Dim dtDay As Long
dtDay = CLng(Day(dtDate))

Select Case dtDay
    Case Is = 1, 21, 31
        formatday = dtDay & "st"
    Case Is = 3, 23
        formatday = dtDay & "rd"
    Case Is = 2, 22
        formatday = dtDay & "nd"
    Case Else
        formatday = dtDay & "th"
End Select
End Function


Example:
? FormatDay(#12/31/1975#)& Format(#12/31/1975#," mmmm yyyy")
31st December 1975

HTH's

thumbup.gif
vtd
Another variation that returns the whole date in the required format:
CODE
Public Function FormatOrdinalDay(ByVal dtDate As Date) As String
  Dim intDay As Integer
  
  intDay = Day(dtDate)
  If (intDay >= 10) And (intDay <= 20) Then
    FormatOrdinalDay = Format(dtDate, "d\t\h mmmm yyyy")
  
  Else
    Select Case intDay Mod 10
      Case 1
        FormatOrdinalDay = Format(dtDate, "d\s\t mmmm yyyy")
      Case 2
        FormatOrdinalDay = Format(dtDate, "d\n\d mmmm yyyy")
      Case 3
        FormatOrdinalDay = Format(dtDate, "d\r\d mmmm yyyy")
      Case Else
        FormatOrdinalDay = Format(dtDate, "d\t\h mmmm yyyy")
    End Select
  
  End If
  
End Function


From the Immediate window:

?FormatOrdinalDay(#12/31/1975#)
31st December 1975
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.