Parsing a JSON date into a C# DateTime

You need to manually wrap your string "programatically" in quotes to ensure that it properly formatted correctly:

string sa = @"""" + "/Date(1409202000000-0500 )/" + @"""";
DateTime dt = JsonConvert.DeserializeObject<DateTime>(sa);

If you need to call it multiple times (which it seems like you do), just move the wrapping responsibility to a method:

public string WrapStringInQuotes(string input)
{
    return @"""" + input + @"""";
}

The issue is with your date string. instead of

string sa = "/Date(1409202000000-0500 )/"

try

string sa = @"""/Date(1409202000000-0500)/""";

Change your code :

 string sa = @"""/Date(1409202000000-0500)/""";
            DateTime dt = new DateTime();
            dt = JsonConvert.DeserializeObject<DateTime>(sa);
            // dt = "2014-08-28 3.00.00 PM"

Tags:

C#

Datetime

Json