Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON date from tweeter to C# format

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 ?

like image 790
flow Avatar asked Feb 19 '13 16:02

flow


People also ask

What is the JSON format of a tweet?

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:

What is the default date format for JSON dates?

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)/.

What do the numbers in the JSON date string mean?

The number within the JSON Date string actually denotes the number on milliseconds that have passed since 01-01-1970 (Unix Epoch time).

What are the fields in Twitter JSON?

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.


2 Answers

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

  • variable jo is a JSON object representing the created_at date property, but effectively the Twitter date string goes into this parameter
like image 61
flow Avatar answered Oct 07 '22 00:10

flow


Part 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"));
like image 45
ddagsan Avatar answered Oct 07 '22 00:10

ddagsan