In this article I will explain how to display ‘st’, ‘nd’, ‘rd’ and ‘th’ suffix after day numbers in Formatted Date string using C# and VB.Net.
By default .Net does not have any provision to display the display ‘st’, ‘nd’, ‘rd’ and ‘th’ suffix after day numbers in Formatted Date string and hence I have come up with a generic method for accomplishing the task.
 
HTML Markup
In order to illustrate the working of the function, I am making use of an ASP.Net TextBox along with ASP.Net AJAX Control Toolkit CalendarExtender to allow user select the date and two Labels to display the formatted date output.
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</cc1:ToolkitScriptManager>
<asp:TextBox ID="txtDate" runat="server" ReadOnly = "true" />
<asp:ImageButton runat="server" ID="imgPopup" ImageUrl="~/Calendar.png" />
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDate"
    PopupButtonID="imgPopup" Format="dd/MM/yyyy" />
<br />
<asp:Button Text="Display" runat="server" OnClick="DisplayFormattedDate" />
<hr />
<asp:Label ID="lbFormattedDate1" runat="server" />
<br />
<br />
<asp:Label ID="lbFormattedDate2" runat="server" />
 
 
Display ‘st’, ‘nd’, ‘rd’ and ‘th’ suffix after day numbers in Formatted Dates
When the Button is clicked, the selected date is first converted to a DateTime object using the respective Culture. For this example, I am making use of dd/MM/yyyy Date Format in the AJAX Calendar Extender and hence the en-GB culture has been used for conversion.
Then the value of the Day from the DateTime object is passed to the GetSuffix method which returns back the appropriate suffix.
The received suffix is then used to format and display the Date in Label control.
For the second Label, I have made use of HTML <sup> tag (Superscript) to display the suffix a little raised.
C#
protected void DisplayFormattedDate(object sender, EventArgs e)
{
    //Convert the Date to DateTime object. Culture for dd/MM/yyyy format is en-GB.
    DateTime dt = Convert.ToDateTime(Request.Form[txtDate.UniqueID], new System.Globalization.CultureInfo("en-GB"));
 
    //Display the date with suffix.
    lbFormattedDate1.Text = string.Format(dt.ToString("dd{0} MMMM yyyy"), GetSuffix(dt.Day.ToString()));
 
    //Display the date with suffix as superscript.
    lbFormattedDate2.Text = string.Format(dt.ToString("dd{0} MMMM yyyy"), "<sup>" + GetSuffix(dt.Day.ToString()) + "</sup>");
}
 
private string GetSuffix(string day)
{
    string suffix = "th";
 
    if (int.Parse(day) < 11 || int.Parse(day) > 20)
    {
        day = day.ToCharArray()[day.ToCharArray().Length - 1].ToString();
        switch (day)
        {
            case "1":
                suffix = "st";
                break;
            case "2":
                suffix = "nd";
                break;
            case "3":
                suffix = "rd";
                break;
        }
    }
 
    return suffix;
}
 
VB.Net
Protected Sub DisplayFormattedDate(sender As Object, e As EventArgs)
   
    'Convert the Date to DateTime object. Culture for dd/MM/yyyy format is en-GB.
    Dim dt As DateTime = Convert.ToDateTime(Request.Form(txtDate.UniqueID), New System.Globalization.CultureInfo("en-GB"))
 
    'Display the date with suffix.
    lbFormattedDate1.Text = String.Format(dt.ToString("dd{0} MMMM yyyy"), GetSuffix(dt.Day.ToString()))
 
    'Display the date with suffix as superscript.
    lbFormattedDate2.Text = String.Format(dt.ToString("dd{0} MMMM yyyy"), (Convert.ToString("<sup>") & GetSuffix(dt.Day.ToString())) + "</sup>")
End Sub
 
Private Function GetSuffix(day As String) As String
    Dim suffix As String = "th"
 
    If Integer.Parse(day) < 11 OrElse Integer.Parse(day) > 20 Then
        day = day.ToCharArray()(day.ToCharArray().Length - 1).ToString()
        Select Case day
            Case "1"
                suffix = "st"
                Exit Select
            Case "2"
                suffix = "nd"
                Exit Select
            Case "3"
                suffix = "rd"
                Exit Select
        End Select
    End If
 
    Return suffix
End Function
 
 
Display ‘st’, ‘nd’, ‘rd’ and ‘th’ suffix after day numbers in Formatted Dates using C# and VB.Net ASP.Net
 
Demo
 
 
Downloads