Solved

formatting dates in VB

Posted on 2000-02-23
8
176 Views
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 .
0
Comment
Question by:Cbell
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 3

Expert Comment

by:paulstamp
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.
0
 
LVL 18

Accepted Solution

by:
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
0
 
LVL 43

Expert Comment

by:TimCottee
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
    Else
        strSuffix = "th"
    End If
    CvtDate = strDay & strSuffix & " " & strDate
End Function
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 43

Expert Comment

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

Author Comment

by:Cbell
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
0
 
LVL 2

Expert Comment

by:p_biggelaar
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!
0
 

Author Comment

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

Expert Comment

by:deighton
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
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

786 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