尝试这个:
string format = "ddd MMM d hh:mm 'GMT'";
dateString = "Tue Oct 8 05:45 GMT";
CultureInfo provider = CultureInfo.InvariantCulture;
try
{
result = DateTime.ParseExact(dateString, format, provider);
}
catch (FormatException)
{
// Date does not conform to format defined
}
如果需要 atry-catch
导致您胃灼热,那么您可以使用TryParseExact()
,如下所示:
string format = "ddd MMM d hh:mm 'GMT'";
dateString = "Tue Oct 8 05:45 GMT";
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime dateValue;
if (DateTime.TryParseExact(dateString, format, provider, DateTimeStyles.None,
out dateValue))
{
// Date conforms to format defined and result is in dateValue variable
}
else
{
// Date does not conform to format defined
}