Timestamp deserialization problem?


Problems with stored Json date and time!

Timestamp of the saved ticks is not the same as received timestamp, when deserialized.
Time is way offshoot.
Below are the two functions from your Utils.cs files.
If you could copy and paste the below code to
C# interactive VS and see if time changes for you too!

#r "System.Web.Extensions"
using System.Text.RegularExpressions ;
using System.Web.Script.Serialization ;
public static DateTime UnixToDateTime(UInt64 unixTimeStamp)
// Unix timestamp is seconds past epoch
DateTime dateTime = new DateTime(1970, 1, 1, 5, 30, 0, 0, DateTimeKind.Unspecified);
dateTime = dateTime.AddSeconds(unixTimeStamp);
return dateTime;
public static string JsonSerialize(object obj)
//ContribStart: was causing exception for MaxJsonLength with default settings so changed it to int.MaxValue
var jss = new JavaScriptSerializer() ;
jss.MaxJsonLength = int.MaxValue ;
jss.RecursionLimit = int.MaxValue ;

string json = jss.Serialize(obj);
MatchCollection mc = Regex.Matches(json, @"\\/Date\((\d*?)\)\\/");
foreach (Match m in mc)
UInt64 unix = UInt64.Parse(m.Groups[1].Value) / 1000;
json = json.Replace(m.Groups[0].Value, UnixToDateTime(unix).ToString());
return json;
var a = JsonSerialize( DateTime.Now ) ;
var b = new JavaScriptSerializer().Deserialize<DateTime>( a ) ;
> a
"\"07-02-2018 15:39:59\""
> b
[02-07-2018 15:39:59]
<--------- day shifts as month. Is this Intended?

Practically i will have to re-engineer whole of the saved data. which is not a problem. just some code to dig into saved dates
Is this a Json quirck. 1st time using Json had always serialized with Xml before. on desktop.

When i deserialize the saved ticks, date is same time is way off.

  • tonystark
    DateTime.ToString() will return the given date in the format as the system has. Which means if you change your date time preferences in the control panel settings it will reflect here as well.

    Also, JsonSerialize function is meant for debugging purpose and not for production purposes. If you want to have some comparison or processing based on the date values you should use the values inside the data structures client library returns directly. If you really want them as strings you should use a fixed format in the ToString(format) function call.
  • trade_then
    Well my system time format is dd-MM-yyyy for short date and dd-MMMM-yyyy for long date.
    and returned string was in format of MM-dd-yyyy.

    So obviously it is not the system. While code is in memory then it can be compared from fields. but stored data is on disk it has to be deserialized and that is where problem lies. Time is assuming various forms.

  • trade_then
    For those who might face the similar problems.
    That is, who are serializing ticks to a disk file. and at deserialization find time to be offset-ted.
    will have to add 5hours and 30 minutes to it.
    for example:- on C# interactive
    #r "System.Web.Extensions"
    using System.Web.Script.Serialization;
    var a = "\"\\/Date(1517975101000)\\/\"" ; // testing timestamp
    > var b = new JavaScriptSerializer().Deserialize<DateTime>( a ) ;
    > b
    [07-02-2018 03:45:01] // time is at -ve offset
    > var e = b.AddHours(5).AddMinutes(30); // added +ve offset
    > e
    [07-02-2018 09:15:01] // now result is good

    apart from other Json date quirks.

Sign In or Register to comment.