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.