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

x
?
Solved

formatting a date

Posted on 2006-10-23
7
Medium Priority
?
159 Views
Last Modified: 2010-04-23
Hi

I have this code for formatting a date

lblDate.Text = pv.StartTime.Value.ToString("ddd, dd MMM")

Thsi produces output such as Monday, 24 October

How would i format this output such that it contains the relevant suffixes for the dates e.g. 1st, 3rd, 24th

thanks
andrea
0
Comment
Question by:andieje
[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
  • +1
7 Comments
 
LVL 13

Expert Comment

by:newyuppie
ID: 17793486
i dont recall any predefined method to achieve this. maybe you need to roll out your own?
something like
if ends in 1 add "st", in 2 "nd" in 3 "rd", else add "th"
0
 
LVL 10

Accepted Solution

by:
Kinger247 earned 2000 total points
ID: 17794407
newyuppie: that route doesn't always work ... ie what about 11th, 12th and 13th.

andieje: Try this:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 1 To 200
            Console.WriteLine(FormatDate(i))
        Next
    End Sub

    Private Function FormatDate(ByVal Day As Integer) As String
        Dim DayFormatted As String = ""

        If Day.ToString.EndsWith("1") And Not Day.ToString.EndsWith("11") Then
            DayFormatted = Day.ToString & "st"
        ElseIf Day.ToString.EndsWith("2") And Not Day.ToString.EndsWith("12") Then
            DayFormatted = Day.ToString & "nd"
        ElseIf Day.ToString.EndsWith("3") And Not Day.ToString.EndsWith("13") Then
            DayFormatted = Day.ToString & "rd"
        Else
            DayFormatted = Day.ToString & "th"
        End If

        Return DayFormatted
    End Function
End Class
0
 
LVL 9

Expert Comment

by:Naveen Swamy
ID: 17795067
0
Industry Leaders: 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!

 

Author Comment

by:andieje
ID: 17795705
kinger, that function looks very good

This is what i had to do to get the date I wanted, it seems very long winded. Am I doing something else wrong

  Dim d As New StringBuilder
        d.Append(rv.StartTime.Value.DayOfWeek.ToString)
        d.Append(", ")
        d.Append(FormatDate(rv.StartTime.Value.Day))
        d.Append(" ")
        d.Append(rv.StartTime.Value.ToString("MMMM"))
        d.Append(" ")
        d.Append(rv.StartTime.Value.Year)
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17795762
kinger, i hadnt thought about 11, 12 and 13 (brain burned)! :)
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 17796691
That looks fine to me andieje  :)

I'd probably do something like this :

With rv.StartTime
        d.Append(.DayOfWeek.ToString)
        d.Append(", ")
        d.Append(FormatDate(.Value.Day))
        d.Append(" ")
        d.Append(.Value.ToString("MMMM"))
        d.Append(" ")
        d.Append(.Value.Year)
End With
0
 

Author Comment

by:andieje
ID: 17804962
THANKS
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

670 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