I'm attempting to add an additional pattern for parsing a swedish year (default is "yy-MM-dd", "yyyy-MM-dd").
The format I want to add is "yyMMdd", just skipping the dividers.
According to the documentation for DateTime.TryParse (http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx):
The string s is
parsed using formatting information in the current DateTimeFormatInfo object,
which is supplied implicitly by the current thread culture. The s parameter
must contain the representation of a date and time in one of the formats returned by the DateTimeFormatInfo.GetAllDateTimePatterns() method
of the current culture.
So my solution was to create a custom instance of
Swedish culture, and adding my format for parsing short DateTime.
As I understand the documentation this should work,
but parsing doesnt work. The strange thing is that displaying a DateTime in the debugger now uses my format, the same thing goes for ToShortDateString().
Any suggestions, or is this a bug?
string date = "100901"; // The first of September 2010
var culture = new CultureInfo("sv-SE", true);
Thread.CurrentThread.CurrentCulture = culture;
var dtf = culture.DateTimeFormat;
var allFormats = dtf.GetAllDateTimePatterns('d').ToList();
View Complete Post