15

How can i get below mentions date format in c#.

  • For 1-Nov-2010 it should be display as : 1st November

  • For 30-Nov-2010 it should be display as : 30th November

Can we do using any date format or make a custom function that returns for 1 -> 'st', 2-> 'nd' 3-> 'rd', any date no -> 'th'.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Pankaj Agarwal
  • 11,191
  • 12
  • 43
  • 59

6 Answers6

32

The following code is based on that answer that generates an ordinal from an integer:

public static string ToOrdinal(int number)
{
    switch(number % 100)
    {
        case 11:
        case 12:
        case 13:
            return number.ToString() + "th";
    }

    switch(number % 10)
    {
        case 1:
            return number.ToString() + "st";
        case 2:
            return number.ToString() + "nd";
        case 3:
            return number.ToString() + "rd";
        default:
            return number.ToString() + "th";
    }
}

Than you can generate your output string:

public static string GenerateDateString(DateTime value)
{
    return string.Format(
        "{0} {1:MMMM}",
        ToOrdinal(value.Day),
        value);            
}
Community
  • 1
  • 1
2

Somthing like this should work...

using System;
using System.Text;

namespace Program {


class Demo { 
      static string[] extensions = 
      //    0     1     2     3     4     5     6     7     8     9
         { "th", "st", "nd", "rd", "th", "th", "th", "tn", "th", "th",
      //    10    11    12    13    14    15    16    17    18    19
           "th", "th", "th", "th", "th", "th", "th", "tn", "th", "th",
      //    20    21    22    23    24    25    26    27    28    29
           "th", "st", "nd", "rd", "th", "th", "th", "tn", "th", "th",
      //    30    31
           "th", "st" };

  public static void Main() {
     String strTestDate = "02-11-2007";
     DateTime coverdate = DateTime.ParseExact(strTestDate, "dd-MM-yyyy", null);
     string s = coverdate.ToString(" MMMM yyyy");
     string t = string.Format("{0}{1}",coverdate.Day,extensions[coverdate.Day]);
     string result = t + s;


     }
   }
}
Nikhil Vaghela
  • 920
  • 2
  • 11
  • 36
2

So here is a fullish solution with extension methods. Works for C# 3.0 and above. Mostly plagiarized Nikhil's work:

public static class DateTimeExtensions
{
        static string[] extensions = // 0 1 2 3 4 5 6 7 8 9 
            { "th", "st", "nd", "rd", "th", "th", "th", "tn", "th", "th", 
                // 10 11 12 13 14 15 16 17 18 19 
                "th", "th", "th", "th", "th", "th", "th", "tn", "th", "th", 
                // 20 21 22 23 24 25 26 27 28 29 
                "th", "st", "nd", "rd", "th", "th", "th", "tn", "th", "th", 
                // 30 31 
                "th", "st" 
            };
        public static string ToSpecialString(this DateTime dt)
        {
            string s = dt.ToString(" MMMM yyyy");
            string t = string.Format("{0}{1}", dt.Day, extensions[dt.Day]);
            return t + s;
        }
}

Test/Use Like this:

Console.WriteLine(DateTime.Now.ToSpecialString());
Console.WriteLine(new DateTime(1990, 11, 12).ToSpecialString());
Console.WriteLine(new DateTime(1990, 1, 1).ToSpecialString());
Console.WriteLine(new DateTime(1990, 1, 2).ToSpecialString());
Console.WriteLine(new DateTime(1990, 1, 3).ToSpecialString());
Console.WriteLine(new DateTime(1990, 1, 4).ToSpecialString());
Console.WriteLine(new DateTime(1990, 12, 15).ToSpecialString());
Console.WriteLine(new DateTime(1990, 8, 19).ToSpecialString());
Console.WriteLine(new DateTime(1990, 9, 22).ToSpecialString());
Console.ReadKey();

Hope that Helps.

gideon
  • 19,329
  • 11
  • 72
  • 113
0

I followed the string example blog from JSL vscontrol and it has a bug at the end he forgot to concatenate the day number at the beginning of the line so it should be

  return strNum + ordinal + str;

and not !

  return ordinal + str;
  • private static string Ordinal(DateTime date) { int dayvalue = Convert.ToInt32(date.Day); string strNum = dayvalue.ToString(); string ordinal = string.Empty; if (strNum.EndsWith("0") || strNum.EndsWith("4") || strNum.EndsWith("5") || strNum.EndsWith("6") || strNum.EndsWith("7") || strNum.EndsWith("8") || strNum.EndsWith("9")) { ordinal = "th"; } else if (strNum.EndsWith("1")) { ordinal = "st"; } else if (strNum.EndsWith("2")) { ordinal = "nd"; } else { ordinal = "rd"; } string str = date.ToString(" MMMMM yyyy"); return ordinal + str; } – Robert Peter Bronstein Sep 14 '15 at 02:25
0

I'm pretty sure there's no datatime routine to show the date as 1st or 30th.

I recently wrote some code like that from scratch. I think you'll need to do the same.

I don't have my code handy but I just created a string array with the letters for each number ("th", "st", "nd", "rd", "th", etc.). Then mod against 10 and use the remainder as an index into the array. You can just append that string to your number.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
0

You can use Regular Expressions to extract the day and month. Then, store all the names of the months in an array and use the .startsWith to obtain the proper name of the month. You can use a simple case to see if you need the 'st', 'nd', 'rd' or 'th'.

npinti
  • 51,780
  • 5
  • 72
  • 96