0

In my ajax success I am getting result.Date as "/Date(-2208967200000)/". I need to check with the following date and proceed..

How to convert the "/Date(-2208967200000)/" to "01-01-1900" for below if condition?

if (result.Date != "01-01-1900") {
....
}
user3747256
  • 113
  • 1
  • 1
  • 13
  • WHat is date in "/Date(-2208967200000)/" ?? – Anubhav Chaudhary Jan 16 '15 at 10:01
  • 1
    If you're getting the date back from an AJAX request, why not send back a valid date format (ECMA sepcifies how a date should be stringified for a reason): [see `Date.toJSON` docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON) – Elias Van Ootegem Jan 16 '15 at 10:14
  • @EliasVanOotegem, I achieved the goal by following the `Paresh Makwana's` answer. – user3747256 Jan 16 '15 at 10:17
  • 1
    @user3747256: I know, just saying that, even though it works in your specific case, a more reliable, and permanent solution would be to ensure the response actually sends back the correct format – Elias Van Ootegem Jan 16 '15 at 10:19

3 Answers3

1

Reference

var jsonDate = "/Date(-2208967200000)/";
var date = new Date(parseInt(jsonDate.substr(6)));
alert(date);

The substr function takes out the "/Date(" part, and the parseInt function gets the integer and ignores the ")/" at the end. The resulting number is passed into the Date constructor.

jQuery dateFormat is a separate plugin. You need to load that explicitly using a tag.

Community
  • 1
  • 1
Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
1

You can convert result.Date into you comparison date format, same as below example

var dateString = "\/Date(-2208967200000)\/".substr(6);
var currentTime = new Date(parseInt(dateString ));
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var date = day + "/" + month + "/" + year;

After doing this.. you can compare it with other date..

Paresh Makwana
  • 189
  • 2
  • 14
  • FYI parseInt will fail as the first character of `dateString` is not a valid integer. – Rory McCrossan Jan 16 '15 at 10:04
  • Possibly you can try this... var dateString = "\/Date(2208967200000)\/".substr(6)... Remove minus sign from bracket.. – Paresh Makwana Jan 16 '15 at 10:06
  • If you remove the minus sign you change the date. – Rory McCrossan Jan 16 '15 at 10:07
  • After little modification from your answer, I achieved using the following code: `var dateString = result.ImplementationDate.substr(6); var currentTime = new Date(parseInt(dateString)); var month = currentTime.getMonth() + 1; var day = currentTime.getDate(); var year = currentTime.getFullYear(); var date = day + "-" + month + "-" + year;` – user3747256 Jan 16 '15 at 10:12
0

You could use a regex to get the value between the brackets and then pass that to the Date():

var input = "/Date(-2208967200000)/";
var matches = /\(([^)]+)\)/.exec(input);
var date = new Date(parseInt(matches[1], 10));

Example fiddle

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • I am getting the date as Mon Jan 01 1900 00:00:00 GMT-0600 (Central Standard Time). How I can compare that date value to "01-01-1900". – user3747256 Jan 16 '15 at 10:05
  • You need to convert that string to a date too so you can compare them, tty this: http://jsfiddle.net/1j5t1z9o/1/ – Rory McCrossan Jan 16 '15 at 10:07