Search code examples
c#jsonjson.netdate-format

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 ?


Solution

  • 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