0

My problem is I have to parse a date in the format

Tue Oct 8 05:45 GMT

but what I always get using DateTime.parse() is:

system.formatexception the datetime represented by the string is not supported in calendar system.globalization.gregoriancalendar

how can this issue can be resolved?

4

1 回答 1

3

尝试这个:

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
}
于 2013-11-02T01:06:17.023 回答