As read from your comments, x.msDate is a string that is stored in a database. If this string can be formatted for different cultures and you need a bit flexibility when parsing, you can use the TryParse method. There is an overload that takes a IFormatProvider as an input. If you want to parse that contains a DateTime in a format that differs from the format of the current culture, you can create a CultureInfo and use its DateTimeFormat property when parsing the DateTime:
DateTime dt;
if (!DateTime.TryParse(stringValue, out dt)
{
if (!DateTime.TryParse(stringValue, new CultureInfo("en-US").DateTimeFormat, DateTimeStyles.None, out dt)
throw new ArgumentException("Unable to parse date");
}
// If you reach this line, you were able to parse the DateTime.
Also you could create a a list of cultures and try to parse the string with the various culture settings. Once the string was parsed successfully, you return the value:
public DateTime ParseString(string value)
{
CultureInfo[] cultures = {CultureInfo.CurrentCulture,
new CultureInfo("en-US"),
new CultureInfo("de-DE")};
foreach(var cult in cultures)
{
DateTime dt;
if (DateTime.TryParse(value, cult, DateTimeStyles.None, out dt))
return dt;
}
throw new ArgumentException(
string.Format("Unable to parse DateTime for string {0}.", value));
}