Solved

formatting a date

Posted on 2006-10-23
7
141 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 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now