I want to convert a DateTime variable to the following format: "Thursday, 26th July 2014".
What would be the right approach.
Thanks
I want to convert a DateTime variable to the following format: "Thursday, 26th July 2014".
What would be the right approach.
Thanks
I think you need to separate the parts before and behind th
from each other:
DateTime dt = new DateTime(2014, 07, 26);
string result = string.Format("{1}{0} {2}",
dt.Day == 1 ? "st" : dt.Day == 2 ? "nd" : dt.Day == 3 ? "rd" : "th",
dt.ToString("dddd, dd", CultureInfo.InvariantCulture),
dt.ToString("MMMM yyyy", CultureInfo.InvariantCulture));
If you don't really need your exact format you can also use DateTime
methods like ToLongDateString
which uses the current culture and has a fixed format.