0

I have searched all over the place for this solution but couldnt find it

DateTime now=DateTime.Now;

I want to convert this to

18th March

Currently I am using

MessageBox.Show(DateTime.Now.ToString("d MMMM"));

Which shows

18 March.

For 1 it Should show 1st

2-2nd

3-3rd

4-4th

Any Solutions?

Nitin Varpe
  • 10,450
  • 6
  • 36
  • 60
  • What cultures do you need to support? I don't believe there's anything to easily support ordinals like this in .NET. If you *only* need English, I'd use a simple array. – Jon Skeet Mar 18 '14 at 07:12
  • Don't forget the 'of' keyword if you use 'st','nd', 'rd' and 'th'. 4th _of_ March – RvdK Mar 18 '14 at 07:12
  • There's no [format specifier](http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx) to obtain ordinal numbers - you'll have to write the function yourself, for the cultures you want to support. – Damien_The_Unbeliever Mar 18 '14 at 07:14
  • You can refer this.. http://stackoverflow.com/questions/9601593/how-can-i-format-07-03-2012-to-march-7th-2012-in-c-sharp – Jameem Mar 18 '14 at 07:14

2 Answers2

3

You might have to use function like this,

  static string ToEnglishOrdinal (int number) {
      if ((number % 100 < 10) || (number % 100 >= 14)) {
        switch (number % 10) {
          case 1: {
            return number.ToString () + "st";
          }

          case 2: {
            return number.ToString () + "nd";
          }

          case 3: {
            return number.ToString () + "rd";
          }
        }
      }

      return number.ToString () + "th";
    }
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
2

Something like this would help you: (untested to give an idea)

DateTime dt = DateTime.Now;
int day = dt.Date.Day % 10;

MessageBox.Show(dt.ToString("d" + ((day == 1) ? “st” : (day == 2) ? “nd” : (day == 3) ? “rd” : (day == 4) ? “th” : “th”) + "MMMM"));
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56