Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

formatting a date

Posted on 2006-10-23
7
Medium Priority
?
161 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
  • 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview

926 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