0

I am using JSON.NET's LINQ to JSON to parse data, which includes dates. My sample JSON looks like this:

{"2013" : [
    { "date":"2/5/2013 11:13 AM","id":23 }],
"2012" : [
    { "date":"28/9/2012 5:29 PM","id":24 }]
}

The format of the date is in d/m/yyyy h:mm. I tried parsing it first using DateTime.Parse:

var postTitles = jobject.Children().SelectMany(x => x.First).
    Select(p =>
    new Report
        {
            Date = DateTime.Parse((string)p["date"]),
            Height = (int)p["height"]
        });

However, it throws a FormatException on the second date so I tried using DateTime.ParseExact instead:

var postTitles = jobject.Children().SelectMany(x => x.First).
    Select(p =>
    new Report
        {
            Date = DateTime.ParseExact(p["date"].ToString(), "dd/mm/yyyy hh:mm", new
                CultureInfo("en-US")),
            Height = (int)p["height"]
        });

This time, I get a FormatException on the first date. Are there any alternatives on how to parse this dates? I tried using Extension Methods as I am used to doing XML but it seems my extension methods are not recognized.

1 Answer 1

4

Your format string for ParseExact is very broken. Basically, you really need to pay attention to the format of your actual data, and look at the MSDN documentation for custom date/time strings. You can't just put any old pattern in and expect it to work: it's got to match your data.

So you probably want a format string of "d/M/yyyy h:mm tt". I would also suggest using the invariant culture:

Date = DateTime.ParseExact(p["date"].ToString(), "d/M/yyyy h:mm tt",
                           CultureInfo.InvariantCulture);

(The invariant culture is mostly the same as the US culture, but the important aspect is that it indicates that this is intended for machine-generated data rather than genuinely US-centric data.)

There may well be a better way of doing this in JSON.NET, but that should at least be a starting point.

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

6 Comments

Can you please enlighten me with regards to format string? I'm quite confused as the sample usage in the MSDN documentation are for ToString.
@CCCC: It's hard to give you any further enlightenment without knowing where your confusion is.
From what I understand, the format specifiers extract specific parts of the date like month, day, and year, which would require a properly formatted date. The thing that I am trying to do is convert the data found in the JSON file to a properly formatted date.
@CCCC: The format specifiers basically describe the format of your data. I've only just noticed an earlier edit removed the sample format - I've reinstated that part, but really you should look carefully through the documentation, and look at the format you were providing vs the format which your data is actually in... they're very different.
I'm sorry for the confusion, I pasted the wrong snippet as I was trying to work on other data to test if there was something wrong with my formatting. Corrected it as what I used originally.
|

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.