3

This is a very strange date fromat I have never seen before coming back from some API in JSON.

"Tue Aug 04 2015 00:17:38 GMT+0000 (UTC)"

That is generating the following error:

System.FormatException: String was not recognized as a valid DateTime.

Which is understandable when using the following method to parse:

DateTime.Parse(x.process_date.Value)

Anyone dealt with complex date formats that may know how to parse that?

2
  • assuming it's always the same format, how about changing the string into a different format, before parsing it? Commented Aug 7, 2015 at 19:53
  • I mean of course its not impossible but that's not super clean. I am sure there is a method for converting this type of thing in .NET Commented Aug 7, 2015 at 19:54

1 Answer 1

7

You can use the DateTime.ParseExact method (or DateTime.TryParseExact, to cleanly handle parsing failures) to accomplish this. These methods allow you to explicitly specify the format string.

Something like this could work:

var dateString = "Tue Aug 04 2015 00:17:38 GMT+0000 (UTC)";
var format = "ddd MMM dd yyyy HH:mm:ss GMT+0000 (UTC)";

var parsed = DateTime.ParseExact(
    dateString, 
    format, 
    System.Globalization.CultureInfo.InvariantCulture);

Or, using TryParseExact:

DateTime parsed;
if (DateTime.TryParseExact(
   dateString, 
   format, 
   System.Globalization.CultureInfo.InvariantCulture, 
   DateTimeStyles.None, 
   out parsed) 
{
   // parsing was successful
}
else
{
   // parsing failed
}

Here's a breakdown of the format string used here:

  • ddd - The abbreviated name of the day of the week.
  • MMM - The abbreviated name of the month.
  • dd - The day of the month, from 01 through 31.
  • yyyy - The year as a four-digit number.
  • HH:mm:ss - The hour, using a 24-hour clock from 00 to 23; the minute, from 00 through 59; and the second, from 0 through 59 (delimited by : characters).
  • GMT+0000 (UTC) - just static text that the format string assumes will always be present. This is pretty brittle and could cause your parsing to fail if the API ever returns different text here. Consider truncating this text, or using NodaTime which offers great support for time zones.

You might need to tweak this format string slightly to your usage -- for example, it wasn't clear from your question whether or not you are using a 12-hour clock or a 24-hour clock.

For more information on how to build a format string, see Custom Date and Time Format Strings on MSDN.

Alternatively, you could eschew using System.DateTime in favor of NodaTime. I'm less familiar with NodaTime myself, but great documentation is available both here on StackOverflow and on NodaTime's site.

Sign up to request clarification or add additional context in comments.

6 Comments

Also be aware that if the last bit is changing you might need to look into using either the K or zzz modifiers, or plain and simply cut some of the text off, i.e. cut off (UTC) and parse the GMT+0000. Take care when cutting that you don't loose information, but my guess is that the (UTC) is superfluous and redundant, if you respect the GMT+0000 part.
@holroy Yeah, you could hardcode it to look for Tue, Aug or 2015 as well as GMT+0000 but that seems like a bad idea :)
@Rawling, if you look at the answer and my comment, you will notice that I'm only referring to the last part. The three element you refer to are already dynamic, whilst the latter part of GMT+0000 (UTC) is hardcoded in the date format string given by Donut. This latter part the OP needs to look in his data set to see if they are changing, and then decide how to handle. If they are changing, Donut's answer will throw exceptions (as I'm quite sure he is aware of).
@holroy Yeah, I was agreeing with you :) If you're going to hardcode that last part, it might not go any better for you than if you hardcode other parts.
FYI, I've updated my answer to draw attention to the fact that the last part of the format string is pretty brittle.
|

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.