issue with datetime jquery asp.net mvc

2019-03-07 05:22发布

问题:

I have an issue with date.In my Model class I have used DateTime property(I used Code First), for transferring json data from action to another action I use Jquery ($.ajax), my date convert in this format, I think it milliseconds:

/Date(1188594000000)/

I tryed to convert it using js, not working: var date = new Date(mydate);

回答1:

/Date(1188594000000)/ is a string and the long numbers inside the brackets are the milliseconds since the beginning of the unix epoch time. You cannot pass that(the string as it is) to Date constructor. If you want to generate a datetime object from that value, you should remove the first 6 characters (/Date() and pass the milliseconds only

var mydate='/Date(1188594000000)/';
var dateVal= parseInt(mydate.substr(6));
var dateObj= new Date(dateVal);
console.log(dateObj);

The statement mydate.substr(6) will return a string value like "1188594000000)/" and passing this to parseInt method returns the number 1188594000000 which can be safely passed to the Date constructor.