Solved

formatting dates in VB

Posted on 2000-02-23
8
180 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
[X]
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
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Suggested Courses
Course of the Month6 days, 1 hour left to enroll

627 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