How to read DateTimeOffset, serialized DataContractJsonSerializer

I serialized an object containing the current date in the DateTimeOffset data type using DataContractJsonSerializer. This is what I get as output:

<root type="object"> <blah type="object"> <DateTime>/Date(1315565372414)/</DateTime> <OffsetMinutes type="number">300</OffsetMinutes> </blah> </root> 

How can I understand that? How do I convert the number 1315565372414 to a date? My client gets this thing in python and would like to change it for today. Iโ€™m not sure if these are tics or seconds from the era, but both of them give the wrong result. Below is my code in .net to convert it, but the results are absurd

 Console.WriteLine(new DateTime(1970, 1, 1).AddTicks(1315565372414)); 

If I try AddSeconds; it throws an exception out of range.

This is how I serialize the date:

 [DataContract] public class Test { [DataMember] public DateTimeOffset blah { get; set; } } var serializer = new DataContractJsonSerializer(typeof(Test)); var writer = new StringWriter(); serializer.WriteObject(new XmlTextWriter(writer), new Test() { blah = DateTimeOffset.Now }); string output = writer.ToString(); Console.WriteLine(output); 
+4
source share
1 answer

This is the number of milliseconds since the era. To deserialize this, we must do the following:

 var date = new DateTime(1970, 1, 1).AddMilliseconds(1315565098519); var dateWithOffset = new DateTimeOffset(date, TimeSpan.FromMinutes(300)); 

Literature:

+3
source

All Articles