-2

I want the date format as 07th November 2021. Kindly let me know the format.

thanks in advance.

jps
  • 20,041
  • 15
  • 75
  • 79
Kakashi
  • 105
  • 4
  • 1
    Looks like a custom date format, the `th` part I have no clue, but the rest should be quite straighforward. Check [Custom date and time format strings](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings) – Cleptus Jan 12 '22 at 11:08
  • 3
    You will have to special-case "1st", "2nd" and "3rd", for days > 3 others: "dd'th' MMMM yyyy" – Klaus Gütter Jan 12 '22 at 11:08
  • @SᴇM quite close, but that would match `07` but not `07th`. – Cleptus Jan 12 '22 at 11:09
  • 2
    You've had excellent service/swift answers. Might you want to consider voting the answers and accepting one. – Peter Smith Jan 12 '22 at 11:25
  • 1
    https://stackoverflow.com/questions/2050805/getting-day-suffix-when-using-datetime-tostring – JamesS Jan 12 '22 at 11:28
  • Looks like the Ames Bond format. – Hans Passant Jan 12 '22 at 11:47

2 Answers2

2

The tricky part is the "th", becasue it has to mutate to "st"/"nd"/"rd" for the some days of a month.

So these days require special-casing of this ordinal suffix, e.g:

string ordinalSuffix;
if (date.Day == 1 || date.Day == 21 || date.Day == 31)
    ordinalSuffix = "st";
else if (date.Day == 2 || date.Day == 22)
    ordinalSuffix = "nd";
else if (date.Day == 3 || date.Day == 23)
    ordinalSuffix = "rd";
else
    ordinalSuffix= "th";

string formatted = date.ToString($"dd'{ordinalSuffix}' MMMM yyyy");

You surely are aware that this works for English only. To support other languages, you might consider using a library like Humanizer.

Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36
1

Try to use this function here:

        public static string ToStringWithSuffix(this DateTime dt, string format) {   
        // The format parameter MUST contain [$suffix] within it, which will be replaced.   
        int day = dt.Day; string suffix = "";   
        // Exception for the 11th, 12th, & 13th   
        // (which would otherwise end up as 11st, 12nd, 13rd)   
        if (day % 100 >= 11 && day % 100 <= 13) {   
            suffix = "th";   
        }else{
            switch (day % 10) {   
                case 1: 
                    suffix = "st";   
                    break;   
                case 2:
                    suffix = "nd";   
                    break;   
                case 3:   
                    suffix = "rd";   
                    break;  
                default:   
                    suffix = "th";   
                    break;   
            }
        }
        // Convert the date to the format required, then add the suffix.   
    return dt.ToString(format).replace("[$suffix]",suffix);
}

You can call it like so:

DateTime(2021, 2, 21);
Console.WriteLine(dt.ToStringWithSuffix("dd'[$suffix]' MMMM yyyy"));

You can also check the documentation on Custom date and time format strings if you need other formats.

Source: https://gist.github.com/woodss/006f28f01dcf371c2bd1

Cleptus
  • 3,446
  • 4
  • 28
  • 34
  • 1
    I like your logic, but consider adding a sample call, something like `DateTime dt = new DateTime(2021, 2, 21); Console.WriteLine(dt.ToStringWithSuffix("dd[$suffix] MMMM yyyy"));` – Cleptus Jan 12 '22 at 11:23
  • Thanks for the suggestion. I will consider your advice and I will edit my answer! – Alex Florin Ciuciulete Jan 12 '22 at 11:25