Reputation: 1895
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?
Upvotes: 0
Views: 140
Reputation: 34846
Try this:
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
}
If the need for a try-catch
causes you heartburn, then you can use TryParseExact()
, like this:
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
}
Upvotes: 3