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 a
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'.
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;
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;
}
}
}
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.
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);
}
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.