I can convert a string value in YYYYMMDD format to a human-friendlier string like so:
string beginDate = GetElement(3, fileBaseName);
string endDate = GetElement(4, fileBaseName);
beginDate = DateTime.ParseExact(beginDate,
"yyyyMMdd",
CultureInfo.InvariantCulture).ToShortDateString();
endDate = DateTime.ParseExact(endDate,
"yyyyMMdd",
CultureInfo.InvariantCulture).ToShortDateString();
return string.Format("{0} to {1}", beginDate, endDate);
But using ToShortDateString() as above, I get the not-as-friendly-as-desired format "4/1/2016 to 4/30/2016"
When I try ToLongDateString(), it goes too far the other direction, expanding the numbers into month names (which I want), but also providing the long form of the day of the week, so that the same values are displayed as "Friday, April 01, 2016 to Saturday, April 30, 2016"
What I really want is for the date range to be displayed as "April 1, 2016 to April 30, 2016" (or "April 1st, 2016 to April 30th, 2016")
Is there a middle ground between ToShortDateString() and ToLongDateString() that I can use, or will I need to "roll my own" to get this?
No, there's nothing between those two within .NET - if you look at DateTimeFormatInfo
you'll see ShortDatePattern
and LongDatePattern
, but no "medium date pattern".
If you know all the cultures you'll need to handle, you could customize the display for each of them - but I would avoid specifying a custom format string where you don't know about the culture in question. (If you know some cultures but not others, you could hard-code the format string for those you know, and just use the long or short pattern for the others - so it wouldn't give quite as nice an experience, but at least you wouldn't be doing something massively culturally inept inadvertently.)
See more on this question at Stackoverflow