Solved

formatting a date

Posted on 2006-10-23
7
143 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

822 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