Nodejs Date Different differenceInYears(obj)

Here you can find the source of differenceInYears(obj)

Method Source Code

var ONE_HOUR_IN_MILLIS = 60*60*1000;
var ONE_DAY_IN_MILLIS = 24*60*60*1000; // hours*minutes*seconds*milliseconds

Date.prototype.differenceInYears = function(obj) {
    var diffDays = Math.round(Math.abs((this.getTime() - obj.getTime())/(ONE_DAY_IN_MILLIS)));
    var diffYears = Math.ceil(diffDays/366);
    return diffYears;
};

Date.prototype.differenceInDays = function(obj) {
    var diffDays = Math.round(Math.abs((this.getTime() - obj.getTime())/(ONE_DAY_IN_MILLIS)));
    return diffDays;
};

Date.prototype.differenceInHours = function(obj) {
    var diffHours = Math.round(Math.abs((this.getTime() - obj.getTime())/(ONE_HOUR_IN_MILLIS)));
    return diffHours;
};

Date.prototype.addHours= function(h){
    this.setHours(this.getHours()+h);//from   w  w w  .j  a  v a2s . c  o  m
    return this;
}

Related

  1. diffDate(d2)
    Number.prototype.diffDate=function(d2){
        var d1=this;
        var diff1=new Date(d1.dateFormat()).getTime();
        var diff2=new Date(Number(d2).dateFormat()).getTime();
        var msdiff=Math.abs(diff1-diff2);
        var daysDiff=Math.floor(msdiff/(1000*60*60*24));
        return daysDiff;
    
  2. diffDays(date)
    Date.prototype.diffDays = function (date) {
        var diffMs = (this.getTime() - date.getTime()); 
        return Math.round(diffMs / 86400000);
    };
    
  3. diffHours(date)
    Date.prototype.diffHours = function (date) {
        var diffMs = (this.getTime() - date.getTime()); 
        return Math.round(diffMs / 3600000); 
    };
    
  4. diffMinutes(date)
    Date.prototype.diffMinutes = function (date) {
        var diffMs = (this.getTime() - date.getTime()); 
        return Math.round(diffMs / 60000); 
    };
    
  5. diffSeconds(date)
    Date.prototype.diffSeconds = function (date) {
        var diffMs = (this.getTime() - date.getTime()); 
        return Math.round(diffMs / 1000);
    };