简体   繁体   中英

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-1900" for below if condition?

if (result.Date != "01-01-1900") {
....
}

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.

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..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM