formatting dates in VB

How do i format 'now' so that it reads
'23rd february 2000' and changes when say it gets to 1st march ive tried all the existing formats and they don't work .
CbellAsked:
Who is Participating?
 
deightonConnect With a Mentor Commented:
Public Function FormDate(x As Date)

   
FormDate = Format(Day(x), "0") & _
Mid("stndrdthththththththththththththththththstndrdthththththththst", _
(Day(x) - 1) * 2 + 1, 2) & Format(x, " mmmm") & Format(Year(x), " 0000")




End Function
0
 
paulstampCommented:
Format$(Now, "long date") will return "23 February 2000"... I dont know of any built in format that will include the "rd" "st" "th" parth though.
0
 
TimCotteeCommented:
There isn't a format that will do this, paul is correct, however here is a little function which will return the appropriate string.

Public Function CvtDate(dteDate As Date) As String
    strDate = Format(dteDate, "MMMM, YYYY")
    strDay = Format(dteDate, "d")
    strSuffix = ""
    If Len(strDay) = 1 Or (Len(strDay) > 1 And Left(strDay, 1) <> "1") Then
        Select Case Right(strDay, 1)
        Case "1"
            strSuffix = "st"
        Case "2"
            strSuffix = "nd"
        Case "3"
            strSuffix = "rd"
        Case "4", "5", "6", "7", "8", "9", "0"
            strSuffix = "th"
        End Select
    Else
        strSuffix = "th"
    End If
    CvtDate = strDay & strSuffix & " " & strDate
End Function
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
TimCotteeCommented:
deighton, didn't have your post when I added this.
0
 
CbellAuthor Commented:
cheers but ive already got that bit its
the rd th and st bit that i cant do
its a right bugger and the user won't change their mind
0
 
p_biggelaarCommented:
You'll need some built in intelligence to determine whether to use 'rd' 'nd' or 'th'. There is no standard VB function to do that, but you could try something like

Dim xx As Date
xx = InputBox("Enter a valid date", "Test", Now())
Select Case Right(CStr(Day(xx)), 1)
    Case "1"
        MsgBox Day(xx) & "st " & Format(Date, "mmmm yyyy")
    Case "2"
        MsgBox Day(xx) & "nd " & Format(Date, "mmmm yyyy")
    Case "3"
        MsgBox Day(xx) & "rd " & Format(Date, "mmmm yyyy")
    Case Else
        MsgBox Day(xx) & "th " & Format(Date, "mmmm yyyy")
    End Select

good luck!
0
 
CbellAuthor Commented:
yep got it - ingenious little number . I really should try to think more laterally but i am too lazy !!!
0
 
deightonCommented:
My function is designed to put the 'rd' and 'th' in, as is Timcottee's

try msgbox formdate(now) with my code in your form
0
All Courses

From novice to tech pro — start learning today.