Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

formatting dates in VB

Posted on 2000-02-23
8
Medium Priority
?
182 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 150 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
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

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

715 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