DateTime.Parse
uses standard date and time format of your CurrentCulture
. Since your string has (
and )
, you need to use custom date and time parsing with TryParseExact
method (or ParseExact
) with an english-based culture (eg: InvariantCulture
) like;
string s = "11 Jun 2015 (12:10)";
DateTime dt;
if(DateTime.TryParseExact(s, "dd MMM yyyy '('HH:mm')'", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
// 11.06.2015 12:10:00
}
If your string has '
in the begining and at the end, you can escape them with "\\'dd MMM yyyy '('HH:mm')'\\'"
format like;
string s = "'11 Jun 2015 (12:10)'";
DateTime dt;
if(DateTime.TryParseExact(s, "\\'dd MMM yyyy '('HH:mm')'\\'", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
// 11.06.2015 12:10:00
}