This project has moved and is read-only. For the latest updates, please go here.

Looking for help with DateTime conversion

Jun 8, 2010 at 9:37 PM

Sorry but I am a bit new at C#.   I have found most of your deserializing straight forward. But I am having issues trying to figure out how to convert from ISO.  Here is the code I am using right now:

                    //A simplified version of the API data
                    string JSON = @"{""date_lastcheck"":""2010-05-31T15:10:09+00:00""}";
                    JObject o = JObject.Parse(JSON);
                    string textDate = (string)o.SelectToken("date_lastcheck");
                    // Returns: 2010-05-31T15:10:09+00:00

                    DateTime Date = ???

So now that I am here, I am confused on the correct syntex for the IsoDateTimeConverter.  I am sure it is much simpler then I'm making it out to be.  Thanks to anyone who can give me a tip or provide some sample code.


Thank you James for providing you time and code to all of us wanting to work with JSON and .Net

Jun 10, 2010 at 3:32 AM

The following is from the NUnit Test suite (a great place to find out how to use the stuff)
[Test] public void SerializeDateTimeOffset() { IsoDateTimeConverter converter = new IsoDateTimeConverter(); DateTimeOffset d = new DateTimeOffset(2000, 12, 15, 22, 11, 3, 55, TimeSpan.Zero); string result; result = JsonConvert.SerializeObject(d, converter); Assert.AreEqual(@"""2000-12-15T22:11:03.055+00:00""", result); Assert.AreEqual(d, JsonConvert.DeserializeObject<DateTimeOffset>(result, converter)); }

Jun 14, 2010 at 7:43 PM

Thanks for the reply brewmanz!  Once I'm back from vacation, I'll have to try out your code.