William Duffy

Glasgow Based C# ASP.NET Web Developer

Ordinal Suffix DateTime Extension Method

In my opinion the most annoying missing piece of date functionality in the .NET framework is an ability to get a DateTime’s ordinal suffix. If you’re as disheartened as me with this then dismay no more. Below is my DateTime extension method that returns the ordinal suffix for the current day of the month.

I’ve put the extension directly into the System namespace, but you can put it somewhere else if you prefer. Now if only we could get this into the frameworks inbuilt date formatting strings, that would be cool!

 
namespace System
{
        ///<summary>
        ///Returns the ordinal suffix for the day of the month represented by this instance
        ///</summary>
        ///<returns></returns>
        public static string OrdinalSuffix(this DateTime datetime)
        {
            int day = datetime.Day;
 
            if (day % 100 >= 11 && day % 100 <= 13)
                return String.Concat(day, "th");
 
            switch (day % 10)
            {
                case 1:
                    return String.Concat(day, "st");
                case 2:
                    return String.Concat(day, "nd");
                case 3:
                    return String.Concat(day, "rd");
                default:
                    return String.Concat(day, "th");
            }
        }
}

Tagged as , , , , + Categorized as C#

3 Comments

  1. Thank you. Didn’t want to write this myself. :)

  2. Rewrote it a bit:

    Initiation:

    DateTime dt = new DateTime();
    dt = DateTime.Now;
    string result = OrdinalSuffix(dt, “{0:dddd, MMM d[OS] – }”);

    Function:

    static string OrdinalSuffix(DateTime datetime, string parameters)
    {
    parameters = string.Format(parameters, datetime);
    int day = datetime.Day;

    if (day % 100 >= 11 && day % 100 <= 13)
    return parameters.Replace(“[OS]“, “th”);

    switch (day % 10)
    {
    case 1:
    return parameters.Replace(“[OS]“, “st”);
    case 2:
    return parameters.Replace(“[OS]“, “nd”);
    case 3:
    return parameters.Replace(“[OS]“, “rd”);
    default:
    return parameters.Replace(“[OS]“, “th”);
    }
    }

  3. I edited the extension method to work with a replace from anywhere and works on top of the string parameters already there. Works Perfectly.

    Syntax for use is myDate.ToStringOrdinal(“MMMM d(ORD)”); // June 1st

    public static class DateTimeExtenstions
    {
    public static string ToStringOrdinal(this DateTime datetime, string parameters)
    {
    int day = datetime.Day;

    string suffix = string.Empty;

    if (day % 100 >= 11 && day % 100 <= 13)
    suffix = "th";

    switch (day % 10)
    {
    case 1:
    suffix = "st"; break;
    case 2:
    suffix = "nd"; break;
    case 3:
    suffix = "rd"; break;
    default:
    suffix = "th"; break;
    }

    parameters = parameters.Replace("(ORD)","'" + suffix + "'");
    return datetime.ToString(parameters);
    }
    }

Leave a Reply