Getting day suffix when using DateTime.ToString()

Getting day suffix when using DateTime.ToString()

Asked on December 25, 2018 in C#.
Add Comment


  • 6 Answer(s)

    One can simply create a string with,

    string.Format("{0:dddd dd}{1} {0:MMMM yyyy}", DateTime.Now, (?));
    

    After that we have to supply a “st” for 1, “nd” for 2, “rd” for 3, and “th” for all others and could be in-lined with a “? :” statement like this example,

    (DateTime.Now.Day % 10 == 1 && DateTime.Now.Day != 11) ? "st"
    : (DateTime.Now.Day % 10 == 2 && DateTime.Now.Day != 12) ? "nd"
    : (DateTime.Now.Day % 10 == 3 && DateTime.Now.Day != 13) ? "rd"
    : "th"
    
    Answered on December 25, 2018.
    Add Comment

    Here , we use switch case. This solution would be better

    string GetDaySuffix(int day)
    {
        switch (day)
        {
            case 1:
            case 21:
            case 31:
                return "st";
            case 2:
            case 22:
                return "nd";
            case 3:
            case 23:
                return "rd";
            default:
                return "th";
        }
    }
    
    Answered on December 25, 2018.
    Add Comment

     

    The  Modulo Operator is used here.

    public string CreateDateSuffix(DateTime date)
    {
        // Get day...
        var day = date.Day;
     
        // Get day modulo...
        var dayModulo = day%10;
     
        // Convert day to string...
        var suffix = day.ToString(CultureInfo.InvariantCulture);
     
        // Combine day with correct suffix...
        suffix += (day == 11 || day == 12 || day == 13) ? "th" :
            (dayModulo == 1) ? "st" :
            (dayModulo == 2) ? "nd" :
            (dayModulo == 3) ? "rd" :
            "th";
     
        // Return result...
        return suffix;
    }
    

     

    Answered on December 25, 2018.
    Add Comment
    USE CODE:
    string GetDaySuffix(int day)
    {
        switch (day)
        {
            case 1:
            case 21:
            case 31:
                return "st";
            case 2:
            case 22:
                return "nd";
            case 3:
            case 23:
                return "rd";
            default:
                return "th";
        }
    }
    Answered on January 15, 2019.
    Add Comment

    I found code how to do it but problem is how to do in UIPATH.
    I believe costume activity is the ans. But i really don’t know how to do that .
    Could anyone upload the workflow of it.
    VB code:
    litDate.Text = DatePart(“dd”, Now) & GetDateSuffix(DatePart(“dd”, Now))

    Function GetDateSuffix(ByVal dateIn As Integer) As String

                {
                    case 11:
                    case 12:
                    case 13:
                        return "th";
                }
                switch (integer % 10)
                {
                    case 1:
                        return "st";
                    case 2:
                        return "nd";
                    case 3:
                        return "rd";
                    default:
                        return "th";
                }
            }
        }   
    
        public static class DateTimeExtensions
        {
            public static string ToString(this DateTime dateTime, string format, bool useExtendedSpecifiers)
            {
                return dateTime.ToString(format)
                    .Replace("nn", dateTime.Day.ToOccurrenceSuffix().ToLower())
                    .Replace("NN", dateTime.Day.ToOccurrenceSuffix().ToUpper());
            } 
        }
    }

    Usage:

    return DateTime.Now.ToString("dddd, dnn MMMM yyyy", useExtendedSpecifiers: true);
    // Friday, 7th March 2014

    Note: The integer extension method can be used for any number, not just 1 to 31. e.g.

    return 332211.ToOccurrenceSuffix();
    // th
    
    
    
    Answered on January 15, 2019.
    Add Comment

    VB code:
    litDate.Text = DatePart(“dd”, Now) & GetDateSuffix(DatePart(“dd”, Now))

    //Function GetDateSuffix(ByVal dateIn As Integer) As String

    Answered on January 28, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.