Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting 3

Status
Not open for further replies.

josie2007

Technical User
Apr 14, 2007
90
US
My date field is formatted like this {0:MMMM dd, yyyy} and the result looks like this April 27, 2007.Is it possible to format and get the out put like this April 27th 2007 instead.How can I format it to get that. thanks
 
Sure it is... but you need a custom function like this:

Code:
    Private Function FormatDate(ByVal dDate As Date) As String
        Dim sRet As String = dDate.ToLongDateString
        Dim sParts As String() = sRet.Split(",")
        'turn 01 into just 1
        sParts(1) = sParts(1).Substring(0, sParts(1).Length - 2) & CInt(sParts(1).Substring(sParts(1).Length - 2, 2))
        If sParts(1).Substring(sParts(1).Length - 3, 1) = "1" Then
            'teens
            sParts(1) += "th"
        Else
            Select Case sParts(1).Substring(sParts(1).Length - 2)
                Case "1"
                    sParts(1) += "st"
                Case "2"
                    sParts(1) += "nd"
                Case "3"
                    sParts(1) += "rd"
                Case Else
                    sParts(1) += "th"
            End Select
        End If
        sRet = sParts(1) & sParts(2)
        Return sRet
    End Function

Senior Software Developer
 
Thank you, this will work for me.thanks again.
 
That function gives me an error (Index was outside the bounds of the array) on the following line:
Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Write(FormatDate(System.DateTime.Now))
    End Sub

    Private Function FormatDate(ByVal dDate As Date) As String
        Dim sRet As String = dDate.ToLongDateString
        Dim sParts As String() = sRet.Split(",")
        'turn 01 into just 1
        [!]sParts(1) = sParts(1).Substring(0, sParts(1).Length - 2) & CInt(sParts(1).Substring(sParts(1).Length - 2, 2))[/!]
        If sParts(1).Substring(sParts(1).Length - 3, 1) = "1" Then
            'teens
            sParts(1) += "th"
        Else
            Select Case sParts(1).Substring(sParts(1).Length - 2)
                Case "1"
                    sParts(1) += "st"
                Case "2"
                    sParts(1) += "nd"
                Case "3"
                    sParts(1) += "rd"
                Case Else
                    sParts(1) += "th"
            End Select
        End If
        sRet = sParts(1) & sParts(2)
        Return sRet
    End Function
I'm sure there is an easier way but it will still involve a function as there is no built in method.



____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Just System.DateTime.Now as in the page load event above. It might be something to do with the time as I don't know what you tested it with.

I would have gone with something like this:
Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sb As New StringBuilder
        sb.Append(System.DateTime.Now.ToString("MMMM"))
        sb.Append(" " & GetOrdinal(System.DateTime.Now))
        sb.Append(" " & System.DateTime.Now.Year)
        Response.Write(sb.ToString)
    End Sub

    Private Function GetOrdinal(ByVal CurrentDate As Date) As String
        Select Case CurrentDate.Day
            Case 1, 21, 31
                Return CurrentDate.Day & "st"
            Case 2, 22
                Return CurrentDate.Day & "nd"
            Case 3, 23
                Return CurrentDate.Day & "rd"
            Case Else
                Return CurrentDate.Day & "th"
        End Select
    End Function




____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Hmmm... I don't get an exception with System.DateTime.Now.

Strange.

Anywho... your pattern is better. I wasn't thinking about stopping at 31 like I probably should have. I was thinking up and into the hundreds when it really didn't need to be that complex. I'll have to punish my brain tonight with a beer for that one. LOL!


Senior Software Developer
 
Hmmm... I don't get an exception with System.DateTime.Now.
It could be the time zones that are different? When I look at my date that is passed into the function, it appears in the format:
Code:
#5/9/2007 5:20:08 PM#



____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Mine says:
Code:
#5/9/2007 11:32:48 AM#

What does your "Now.ToLongDateString" say?
Code:
"Wednesday, May 09, 2007"

btw - I'm going to give you a star for the improved pattern above.

Senior Software Developer
 
Since I am getting my data form the databae I am goint to do it this way. to_char(b.datelet ,'fmMonth ddth yyyy')
 
Looks like josie found her own solution. Does she get a star too... LOL!

Good to know that the ToLongDateString might be different though. Are you using VS 2005 with Framework version 2.0 like I am?

The only other difference that I know of is that I tested this inside of a console app and I see you tested in an ASP.Net app. But, that really shouldn't matter as it's the same namespace. It just has to be the framework version... right???

Senior Software Developer
 
Are you using VS 2005 with Framework version 2.0 like I am?
Yes, that's correct.

At a guess I'd say it's down the the PC and what regional/language/time zone settings they have. It's something to bear in mind though as I didn't realise there was a difference.



____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top