1

Hello i want to convert my date like following format-- 1st JUN 2012, 3rd MAR 2012, 11th JUN 2012

    Convert.ToDateTime(ds.Tables[0].Rows[0]["Date"]).ToString("d MMM yyyy")

but with above code i am getting like this

1 JUN 2012, 3 MAR 2012, please help me out. Thank you.

Gerardo Lima
  • 6,467
  • 3
  • 31
  • 47
Pushpendra
  • 174
  • 2
  • 10
  • Check this out, http://stackoverflow.com/questions/20156/is-there-an-easy-way-to-create-ordinals-in-c – SuganR Aug 09 '12 at 13:59
  • Custom date formatting is avalible here http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx however what you are asking about doesn't exist since different langauges use different letters for "st", "nd", "rd" etc. Your going to have to write something yourself. – Mark Broadhurst Aug 09 '12 at 13:59
  • possible duplicate of [Is there an easy way in .NET to get "st", "nd", "rd" and "th" endings for numbers?](http://stackoverflow.com/questions/69262/is-there-an-easy-way-in-net-to-get-st-nd-rd-and-th-endings-for-number) – stusmith Aug 09 '12 at 14:01
  • In spanish ordinals can be represented by º if you are referencing a no sex deterministic word or a masculine word, some sex neutral words are represented with er after int, in femenine is ª Your solution only will be work to an English target, example 1ª puerta, ha quedado 1º o 1er piso (1st door, finished 1st,1st flat – Alberto León Aug 09 '12 at 14:52

7 Answers7

1

Untested, but it should work:

DateTime dateTime = Convert.ToDateTime(ds.Tables[0].Rows[0]["Date"]);
string date = string.Format("{0}{1} {2} {3}", dateTime.Day, GetOrdinal(dateTime.Day), dateTime.ToString("MMM"), datetTime.Year);

public string GetOrdinal(int number)
{
        switch(number % 100)
        {
            case 11:
            case 12:
            case 13:
                    return "th";
        }
        switch(number % 10)
        {
                case 1:
                        return "st";
                case 2:
                        return "nd";
                case 3:
                        return "rd";
                default:
                        return "th";
        }
}
victorvartan
  • 1,002
  • 2
  • 11
  • 31
0

It seems you can't, according to this: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Max Lambertini
  • 3,583
  • 1
  • 19
  • 24
0

There's no way to do this with a format string. You will have to convert the day number by yourself. There's an approach described in this answer.

Community
  • 1
  • 1
MatthiasG
  • 4,434
  • 3
  • 27
  • 47
0

If you are going to use "st", "nd", "rd", "th" etc. there is no other way then using swtich operator:

switch(yourdate.Day % 10)
  case(1): ...
  case(2): ...
Johnny_D
  • 4,592
  • 3
  • 33
  • 63
0

you can try like this,

string strPostfix = string.Empty;
if (DateTime.Now.Day == 1 || DateTime.Now.Day == 21 || DateTime.Now.Day == 31)
    {
    strPostfix = "st";
    }
    else if (DateTime.Now.Day == 2 || DateTime.Now.Day == 22)
    {
    strPostfix = "nd";
    }
    else if (DateTime.Now.Day == 3 || DateTime.Now.Day == 23)
    {
    strPostfix = "rd";
    }
    else
    {
    strPostfix = "th";
    }

Hope this will help you, Thank You.

SuganR
  • 129
  • 1
  • 10
0

Since ordinals are very much language specific, it is not recommended to use them! However, if you must, it should be pretty straightforward to write a switch statement to take care of it. (If it is really important to you)

Then you could split your string at spaces, process the first part, and merge the string again

olagjo
  • 2,125
  • 2
  • 14
  • 16
0

Please, to google a little... there are solutions published http://blog.csharphelper.com/2010/07/15/find-the-ordinal-representation-of-an-integer-1st-2nd-3rd-in-c.aspx

Alberto León
  • 2,879
  • 2
  • 25
  • 24