Skip to content Skip to sidebar Skip to footer

How To Convert Javascript String Format To Date

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

Solution 1:

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

var dateString = "\/Date(-2208967200000)\/".substr(6);
var currentTime = newDate(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..

Solution 2:

Reference

var jsonDate = "/Date(-2208967200000)/";
var date = newDate(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.

Solution 3:

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 = newDate(parseInt(matches[1], 10));

Example fiddle

Post a Comment for "How To Convert Javascript String Format To Date"