Hi,
Try this:
public static string GetOrdinal(string s)
{
int Number = Convert.ToInt32(s.Split(' ')[0]);
string suffix = String.Empty;
if (Number.ToString().Length > 2)
{
int intEndNum = Convert.ToInt32(Number.ToString().Substring(Number.ToString().Length - 2, 2));
if (intEndNum >= 11 && intEndNum <= 13)
switch (intEndNum)
{
case 11:
case 12:
case 13:
suffix = "th";
break;
}
}
if (Number >= 21)
{
int Number21 = Convert.ToInt32(Number.ToString().Substring(Number.ToString().Length - 1, 1));
switch (Number21)
{
case 1:
suffix = "st";
break;
case 2:
suffix = "nd";
break;
case 3:
suffix = "rd";
break;
case 0:
suffix = "th";
break;
default:
for (int i = 4; i <= 9; i++)
{
if (Number21 == i)
{
suffix = "th";
break;
}
else
suffix = String.Empty;
}
break;
}
}
else
{
switch (Number)
{
case 1:
suffix = "st";
break;
case 2:
suffix = "nd";
break;
case 3:
suffix = "rd";
break;
default:
for (int i = 4; i <= 21; i++)
{
if (Number == i)
{
suffix = "th";
break;
}
else
{
suffix = String.Empty;
}
}
break;
}
}
string NewDate = s.Split(' ')[0]+""+suffix + " " + s.Split(' ')[1] + " " + s.Split(' ')[2];
return NewDate;
}
Call the above function from anywhere like:
static void Main()
{
string TodayDate = DateTime.Now.ToString("dd MMM yyyy");
string date = GetOrdinal(TodayDate);
Console.WriteLine(date);
}
--Amit