1

I have an ajax method where i request for a C# object that contains a DateTime. My javascript-console shows that i get the date in the following format:

$.ajax({
   type: "GET",
   url: getDateTestUrl,
   dataType: "json",,
   success: function (response) {
      console.log(response.datetest);
   }
});

Result:

/Date(1454513400000)/

How do I make this into a javascript date? I tried to use both New Date() and Date.parse()

Example form the javascript-console:

Date.parse('/Date(1454513400000)/');
NaN

new Date('/Date(1454513400000)/')
Invalid Date
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Lord Vermillion
  • 5,264
  • 20
  • 69
  • 109
  • 2
    Possible duplicate of [Format a Microsoft JSON date?](http://stackoverflow.com/questions/206384/format-a-microsoft-json-date) – Aleksandr Ivanov Feb 23 '16 at 08:20
  • Check this [answer](http://stackoverflow.com/questions/206384/format-a-microsoft-json-date). You can also configure your C# backend to return dates in better format. Implementation really depends on technology. – Aleksandr Ivanov Feb 23 '16 at 08:22
  • @AleksandrIvanov—actually, a time value is probably the best format, since parsing strings is very problematic. – RobG Feb 23 '16 at 08:37
  • Possible duplicate of [*Convert UNIX to readable date in javascript*](http://stackoverflow.com/questions/28150469/convert-unix-to-readable-date-in-javascript). – RobG Feb 23 '16 at 08:45

1 Answers1

4

If the result is a string that is milliseconds since the ECMAScript epoch (same as UNIX, 1970-01-01T00:00:00Z), you can just grab the digits, convert to number and pass to the Date constructor:

var s = '/Date(1454513400000)/';
document.write(new Date(+(s.replace(/\D/g,''))))

you can use:

RobG
  • 142,382
  • 31
  • 172
  • 209