0

I want to convert a DateTime variable to the following format: "Thursday, 26th July 2014".

What would be the right approach.

Thanks

santubangalore
  • 796
  • 1
  • 12
  • 25

2 Answers2

5

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.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
3

From the MSDN Docs, this produces a "Long" date pattern

DateTime.ToString("D")
Ian
  • 33,605
  • 26
  • 118
  • 198