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?

1
  • How are you calling DateTime.Parse()? Commented Nov 2, 2013 at 1:07

1 Answer 1

3

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Since the date string includes "GMT", consider using DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal instead of DateTimeStyles.None.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.