1

I have myself a DateTime variable of

7/11/2014 

and I want to convert that date to display as

7th November 2014

What format do I use? I have tried ToLongDateString but it misses the suffix of the day date.

Maslow
  • 18,464
  • 20
  • 106
  • 193
Canvas
  • 5,779
  • 9
  • 55
  • 98

1 Answers1

6

I don't believe there's any direct support for ordinals ("st", "nd", "th") within .NET. If you only need to support English, I suggest you hard code it yourself. For example:

string text = string.Format("{0}{1} {2} {3}", dt.Day, GetOrdinal(dt.Day),
                            dt.ToString("MMMM"), dt.Year);

(Where you'd write GetOrdinal yourself.) Note that this assumes you want exactly this format - different cultures (even within English) may prefer November 7th 2014 for example.

If you need to support all kinds of languages, it becomes very difficult - different languages have some very different approaches to ordinals.

Side-note: Even Noda Time doesn't handle this yet. I hope to eventually implement some CLDR support, which should in theory handle it for all locales. We'll see...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Also consider making this format an extension method of DateTime and stick it into your personal standard library. – EtherDragon Sep 30 '14 at 15:16
  • As a side note, [Humanizer](http://humanizr.net/) is a great .NET project for converting numbers into ordinal or cardinal or anything else. – Icemanind Sep 30 '14 at 18:18
  • @icemanind: Interesting. It doesn't seem to be using CLDR data, which seems a little unfortunate, but still an interesting project. – Jon Skeet Sep 30 '14 at 18:22
  • @JonSkeet - I think you are correct. But being an open source project, hopefully, one day, CLDR data can be programmed into it. It'll probably be a large undertaking unfortunately. – Icemanind Sep 30 '14 at 18:30