views:

269

answers:

1

Hello,

I have a JSON Date for example: "\/Date(1258529233000)\/"

I got below code to convert this JSON date to UTC Date in String Format.

var s = "\\/Date(1258529233000)\\/";
 s = s.slice(7, 20);
 var n = parseInt(s);
 var d = new Date(n);
 alert(d.toString());

Now I need to get current UTC Date with Time and convert it into JSON date format.

Then do a date difference between these two dates and get number of minutes difference.

Can someone help me with this?

A: 

javascript Date objects are all UTC Dates until you convert them to strings.

Date.fromJsnMsec= function(jsn){
    jsn= jsn.match(/\d+/);
    return new Date(+jsn)
}

Date.prototype.toJsnMsec= function(){
    return '/Date('+this.getTime()+')/';
}

//test
var s= "\/Date(1258529233000)\/";
var D1= Date.fromJsnMsec(s);
var D2= new Date();
var diff= Math.abs(D1-D2)/60000;
var string= diff.toFixed(2)+' minutes between\n\t'+
D1.toUTCString()+' and\n\t'+D2.toUTCString()+'\n\n'+s+', '+D2.toJsnMsec();
alert(string)
kennebec
It worked. Thanks Kennebec
Sakthi