formatting dates in VB

Posted on 2000-02-23
Last Modified: 2011-09-20
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 .
Question by:Cbell
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2

Expert Comment

ID: 2549812
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.
LVL 18

Accepted Solution

deighton earned 50 total points
ID: 2549829
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
LVL 43

Expert Comment

ID: 2549833
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
        strSuffix = "th"
    End If
    CvtDate = strDay & strSuffix & " " & strDate
End Function
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 43

Expert Comment

ID: 2549836
deighton, didn't have your post when I added this.

Author Comment

ID: 2549837
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

Expert Comment

ID: 2549839
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!

Author Comment

ID: 2549870
yep got it - ingenious little number . I really should try to think more laterally but i am too lazy !!!
LVL 18

Expert Comment

ID: 2549875
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

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS Access Search and Replace Using VBA 6 105
Convert VB6 MSXML2.ServerXMLHTTP process to C# 2 81
Prevent user closing word document opened with VB6 6 89
MsgBox 4 76
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question