Calculate date difference in javascript

Here is the function to calculate date difference in javascript.


var DateDiff = {

    inDays: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();

        return parseInt((t2-t1)/(24*3600*1000));
    },

    inWeeks: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();

        return parseInt((t2-t1)/(24*3600*1000*7));
    },

    inMonths: function(d1, d2) {
        var d1Y = d1.getFullYear();
        var d2Y = d2.getFullYear();
        var d1M = d1.getMonth();
        var d2M = d2.getMonth();

        return (d2M+12*d2Y)-(d1M+12*d1Y);
    },

    inYears: function(d1, d2) {
        return d2.getFullYear()-d1.getFullYear();
    }
}

var dString = "May, 20, 1984";

var d1 = new Date(dString);
var d2 = new Date();

document.write("
Number of days since "+dString+": "+DateDiff.inDays(d1, d2)); document.write("
Number of weeks since "+dString+": "+DateDiff.inWeeks(d1, d2)); document.write("
Number of months since "+dString+": "+DateDiff.inMonths(d1, d2)); document.write("
Number of years since "+dString+": "+DateDiff.inYears(d1, d2));

source : http://stackoverflow.com/questions/7763327/how-to-calculate-date-difference-in-javascript

Ajax call to remote server

you need o use a method that is called as JSONP

one of the best ways is to use jQuery to reduce the code and worries between your page and the server, and all you need to do is:

$.ajax({
  dataType: 'jsonp',
  data: 'id=10',
  jsonp: 'jsonp_callback',
  url: 'http://myotherserver.com/getdata',
  success: function () {
    // do stuff
  },
});