UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
> Date As Words, Any Version    
 
   
CyberCow
post Feb 12 2008, 06:20 AM
Post#1


UdderAccess Admin + UA Ruler
Posts: 19,555
Joined: 27-April 02
From: Upper MI


To get from 1/4/2009 to "January 4th, 2009" or "4th of January, 2009" or even simply "January 4th" just got a little simpler. This date slicer/dicer takes a valid date and turns it into verbose syntax.
special thanks to UA VIP jwhite for pointing out an error with variants of 11, 12 & 13 not getting their "th" - now corrected - March 13, 2011
CODE
Public Function DateAsWords(dt As Date, Optional f As Integer = 0)
' Usage: DateAsWords(#1/4/2009#) or DateAsWords(#1/4/2009#,1)
' dt = a valid date  -   'f' selects a format:
'     DateAsWords(#1/4/2009#)  = "4th"
'     DateAsWords(#1/4/2009#,1) = "January 4th, 2009"
'     DateAsWords(#1/4/2009#,2) = "4th of January, 2009"
'     DateAsWords(#1/4/2009#,3) = "4th of January"
' Make sure your date value is wrapped in #'s or is a valid date variable
' oterwise, you'll just get "1 divided by 4 divided by 2009" (1.24e-4)
' So why do simple numbers, (not even formatted like a date), return a result? . . . .
'
' Access treats dates as 8 byte numbers and balances 12/30/1899
' as the zero point. (Dunno why) If you date format zero
' Format(0,"m/d/yyyy") = 12/30/1899.
' However, if you inadvertantly use only 3 'y's
' you will get: Format(0,"m/d/yyy") = [URL="tel:12/30/99364"]12/30/99364[/URL]
' So, today's numeric date value is basically the number of days
' since 12/30/1899.
' The formula works negatively too. If you Format(-179,"m/d/yyyy")
' you will get 7/4/1899
' -657434 is the negative limit (1/1/100) and 2958465 is the upper
' limit ([URL="tel:12/31/9999"]12/31/9999[/URL])
    Select Case right(Day(dt), 1)
        Case 1
            If Day(dt) > 9 And Day(dt) < 14 Then
                DateAsWords = Day(dt) & "th"
              Else
                DateAsWords = Day(dt) & "st"
            End If
        Case 2
            If Day(dt) > 9 And Day(dt) < 14 Then
                DateAsWords = Day(dt) & "th"
              Else
                DateAsWords = Day(dt) & "nd"
            End If
        Case 3
            If Day(dt) > 9 And Day(dt) < 14 Then
                DateAsWords = Day(dt) & "th"
              Else
                DateAsWords = Day(dt) & "rd"
            End If
        Case Else
            DateAsWords = Day(dt) & "th"
    End Select
    
    Select Case f
        Case 0
            ' Change nothing
        Case 1
            DateAsWords = Format(dt, "mmmm") & " " & DateAsWords & ", " & Year(dt)
        Case 2
            DateAsWords = DateAsWords & " of " & Format(dt, "mmmm") & ", " & Year(dt)
        Case 3
            DateAsWords = DateAsWords & " of " & Format(dt, "mmmm")
    End Select
End Function
Go to the top of the page
 

Posts in this topic



Custom Search
RSSSearch   Top   Lo-Fi    18th December 2017 - 11:47 AM