How to format a JSON date obtained from twitter to a C# DateTime ? Here is the format of the date I receive :
"Tue, 19 Feb 2013 13:06:17 +0000"
Can I do it with JSON.NET ?
Each tweet object comes in JSON format, a mix of ‘root-level’ attributes, and child objects (which are represented with the {} notation). The Twitter developer page gives the following example:
Microsoft .NET Web APIs returns JSON dates in standardized format by default, but the older versions of .Net framework may serialize the c# datetime object into a strange string format like /Date (1530144000000+0530)/ or /Date (1530144000000)/.
The number within the JSON Date string actually denotes the number on milliseconds that have passed since 01-01-1970 (Unix Epoch time).
As we saw, there are multiple fields in the Twitter JSON which contains textual data. In a typical tweet, there’s the tweet text, the user description, and the user location.
Solved with use of DateTime.ParseExact
-> http://blog.kevinyu.org/2012/07/handling-json-in-net.html
Link Update: the linked blog post is offline. It cached copy can still be referenced via the Way Back Machine Internet Archive.
The common .NET code copied from the blog post is:
public const string Const_TwitterDateTemplate = "ddd MMM dd HH:mm:ss +ffff yyyy";
DateTime createdAt = DateTime.ParseExact((string)jo["created_at"],
Const_TwitterDateTemplate, new System.Globalization.CultureInfo("en-US"));
where
jo
is a JSON object representing the created_at
date property, but effectively the Twitter date string goes into this parameterPart of code from flow's answer.
public const string Const_TwitterDateTemplate = "ddd MMM dd HH:mm:ss +ffff yyyy";
DateTime createdAt = DateTime.ParseExact((string)jo["created_at"], Const_TwitterDateTemplate, new System.Globalization.CultureInfo("en-US"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With