ARTICLE

Postfix Dates

Posted by Mark White Articles | How do I September 02, 2008
This tip shows how to add 'st', 'nd', 'rd', 'th' to your date text.
Reader Level:

A simple function to include the appropriate postfix in your date text:

'Monday 1st September 2008' etc

 

protected string TransformDateToText()

{

string strMonth = string.Empty;

string strDayPostfix = string.Empty;

switch (DateTime.Now.Month)

{

case 1: strMonth = "January";

break;

case 2: strMonth = "February";

break;

case 3: strMonth = "March";

break;

case 4: strMonth = "April";

break;

case 5: strMonth = "May";

break;

case 6: strMonth = "June";

break;

case 7: strMonth = "July";

break;

case 8: strMonth = "August";

break;

case 9: strMonth = "September";

break;

case 10: strMonth = "October";

break;

case 11: strMonth = "November";

break;

case 12: strMonth = "December";

break;

default: break;

}

if (DateTime.Now.Day == 1 || DateTime.Now.Day == 21 || DateTime.Now.Day == 31)

{

strDayPostfix = "st";

}

else if (DateTime.Now.Day == 2 || DateTime.Now.Day == 22)

{

strDayPostfix = "nd";

}

else if (DateTime.Now.Day == 3 || DateTime.Now.Day == 23)

{

strDayPostfix = "rd";

}

else

{

strDayPostfix = "th";

}

return DateTime.Now.DayOfWeek.ToString() + " " + DateTime.Now.Day.ToString() + strDayPostfix + " " + strMonth + " " + DateTime.Now.Year.ToString();

}

COMMENT USING