I'm receiving a date and time in this format:
\/Date(1434013505757-0700)\/
I just can't find a way to reformat it to a human readable format.
Thank you in advance,
I'm receiving a date and time in this format:
\/Date(1434013505757-0700)\/
I just can't find a way to reformat it to a human readable format.
Thank you in advance,
You are looking at an Micoroft Date format produced by the DataContractJsonSerializer.
The following example outputs the current date and then the datetime from your example:
var ser = new DataContractJsonSerializer(typeof(Example));
var ex = new Example{ MSDate = DateTime.Now};
// write Example to the stream
var ms = new MemoryStream();
ser.WriteObject(ms, ex);
var cnt = Encoding.UTF8.GetString(ms.ToArray());
Debug.WriteLine(cnt); // show wireformat
ms.Position = 0;
var exdes = (Example)ser.ReadObject(ms); // read back
// create our own json stream and deserialize
var somejsonwithdate = @"{""MSDate"":""\/Date(1434013505757-0700)\/""}";
var yourdate = (Example) ser.ReadObject(
new MemoryStream(Encoding.UTF8.GetBytes(somejsonwithdate)));
Debug.WriteLine("{0:yyyy-MM-dd h:mm:ss", yourdate.MSDate);
and here is the DTO type
public class Example
{
public DateTime MSDate { get; set; }
}
This code outputs in my Debug Output:
{"MSDate":"/Date(1435128933854+0200)/"}
2015-06-11 11:05:05
From the MSDN article Stand-Alone JSON Serialization
DateTime values appear as JSON strings in the form of "/Date(700000+0500)/", where the first number (700000 in the example provided) is the number of milliseconds in the GMT time zone, regular (non-daylight savings) time since midnight, January 1, 1970. The number may be negative to represent earlier times. The part that consists of "+0500" in the example is optional and indicates that the time is of the Local kind - that is, should be converted to the local time zone on deserialization. If it is absent, the time is deserialized as Utc. The actual number ("0500" in this example) and its sign (+ or -) are ignored.
The ASP.NET AJAX client JavaScript code automatically converts such strings into JavaScript DateTime instances. If there are other strings that have a similar form that are not of type DateTime in .NET, they are converted as well.
The conversion only takes place if the "/" characters are escaped (that is, the JSON looks like "/Date(700000+0500)/"), and for this reason WCF's JSON encoder (enabled by the WebHttpBinding) always escapes the "/" character.
This is (best guess) UNIX Timestamp with millisecond resolution and time zone offset.
See here on how to convert between the two. Bear in mind that the example is based on 'Second' resolution, but yours is in 'MilliSecond' resolution. So use AddMilliSeconds.
1434013505757-0700
|<--------->| |--|
Unix TimeZone
TimeStamp Offset
Hope you can figure out the code yourself. Note that time-zone offset is in Hours. Just for reference Unix-Timestamp is defined as the number of seconds (or milliseconds) elapsed since the midnight of 1-Jan-1970 at Greenwich.