Solved

formatting a date

Posted on 2006-10-23
7
153 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 500 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
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

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

628 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