-2

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,

Kourosh
  • 608
  • 2
  • 14
  • 34
  • 2
    Try to describe what format is this. Where is year, day, month... – Giorgi Nakeuri Jun 24 '15 at 06:11
  • Do *you* know what date/time that value is *meant* to represent? If so, please share that information with us. If not, how are you going to assess whether any answer is actually correct? – Damien_The_Unbeliever Jun 24 '15 at 06:23
  • As a general rule, the programmer is responsible to know the source and format of his programs inputs. So it is _your_ responsibility to find out the format. The data must be coming from somewhere some place some organization. You need to traverse the organization tree up to the source of the data. In my exp. asking usually helps. As of us, we can just speculate. – inquisitive Jun 24 '15 at 06:28
  • This is the ancient DataContractSerializer's format, from the early AJAX days when there was no concept of dates in Json, so Microsoft decided to output a UNIX timestamp escaped so that it can't be confused with text or a number. The real solution is to replace DataContractSerializer - the date format is just one of its problems. ASP.NET MVC, Web API, all other REST services use the ISO date format. Otherwise, you'll have to parse the string to an actual date – Panagiotis Kanavos Jun 24 '15 at 07:09

2 Answers2

3

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.

Community
  • 1
  • 1
rene
  • 41,474
  • 78
  • 114
  • 152
1

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.

Community
  • 1
  • 1
inquisitive
  • 3,549
  • 2
  • 21
  • 47
  • This explains the numbers, see [DateTime wire format](https://msdn.microsoft.com/en-us/library/bb412170(v=vs.110).aspx) where the `Date(` and extra slashes come from. – rene Jun 24 '15 at 07:05
  • You are correct, though also keep in mind that the time zone offset is extra data,. The timestamps is UTC based, regardless of what offset is presented. This is slightly different from other formats, such as ISO8601, where the time presented has *already* been adjusted by that offset. Here, it has not. – Matt Johnson-Pint Jun 24 '15 at 19:16