A few days before i got a requirement from my client for date formating which is like
"Saturday - 26th June 2010" well it seems to be a small thing but i faced problem when i start changing
date to something like 26th..21st or just 1st...
After some googling i haven't find any good solution or not any inbuild format for this in C#.
So i just create a method which takes the date as a parameter and return the required date format.
and here is this method:
///
/// This method accept the Date and convert it to the format like: Sunday - 3rd October 2010
///
///
///
private string GetFormatedDate(DateTime dateTime)
{
string dateSuffix = (dateTime.Day.ToString().EndsWith("1"))
? ((dateTime.Day.ToString().StartsWith("1") &&
dateTime.Day != 1)
? "th"
: "st")
: (dateTime.Day.ToString().EndsWith("2"))
? (dateTime.Day.ToString().StartsWith("1")
? "th"
: "nd")
: (dateTime.Day.ToString().EndsWith("3"))
? (dateTime.Day.ToString().StartsWith("1")
? "th"
: "rd")
: "th";
return dateTime.ToString("dddd - d~ MMMM yyyy").Replace("~", dateSuffix);
}
In the above example i used Conditional operator which is something like:
CheckCondition ? If_True_Then_This_Value : Else_This_Value;
And at the last line :
return dateTime.ToString("dddd - d~ MMMM yyyy").Replace("~", dateSuffix);
i just suffix a character '~', it can be anything but not the part of any date format and replaced it with the date suffix (ie. th, st, rd or nd).
Hope this will help you guys when you try to do the same.
Cheers.....