0

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

What would be the right approach.

Thanks

santubangalore
  • 741
  • 1
  • 11
  • 24

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
  • 429,027
  • 67
  • 649
  • 891
3

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

DateTime.ToString("D")
Ian
  • 32,440
  • 24
  • 112
  • 191