Nodejs Date Compare compare(b)

Here you can find the source of compare(b)

Method Source Code

var debug = require('debug')('util/date.js');

// //from  w  w  w .j ava2s . c  o m
// Extending the Date class
//
Date.prototype.compare = function(b){
  // comparing full year
  if(this.getFullYear() != b.getFullYear()){
    return false;
  }else{
    
    // comparing month
    if(this.getMonth() != b.getMonth()){
      return false;
    }else{
    
      // comparing day of month
      if(this.getDate() != b.getDate()){
        return false;
      }else{
        
        // comparing day of week
        if(this.getDay() != b.getDay()){
          return false;
        }else{
          
          // comparing hours
          if(this.getHours() != b.getHours()){
            return false;
          }else{
            
            // comparing minutes
            if(this.getMinutes() != b.getMinutes()){
              return false;
            }else{
              
              // comparing seconds
              if(this.getSeconds() != b.getSeconds()){
                return false;
              }else{
                return true;
              }
            }
          }
        }
      }
    }
  }
};

Related

  1. isSameDate(d)
    Date.prototype.isSameDate = function (d) {
        var tDate = new Date(this.getFullYear(), this.getMonth(), this.getDate());
        var pDate = new Date(d.getFullYear(), d.getMonth(), d.getDate());
        return tDate == pDate
    
  2. isSameDateAs(pDate)
    Date.prototype.isSameDateAs = function (pDate) {
        return (
          this.getFullYear() === pDate.getFullYear() &&
          this.getMonth() === pDate.getMonth() &&
          this.getDate() === pDate.getDate()
        );
    
  3. date()
    Date.prototype.date = function () {
        return String(this.getFullYear()) + "-" + (this.getMonth() + 1).zfill(2) + "-" + this.getDay().zfill(2);
    };
    
  4. date()
    Date.prototype.date = function(){
      var yyyy = this.getFullYear().toString();
      var mm = this.getFullMonth().toString(); 
      var dd  = this.getDate().toString();
      return mm+'-'+dd+'-'+yyyy;
    };
    
  5. date()
    Date.prototype.date = function() {
      var y = this.getFullYear(),
          m = this.getMonth(), 
          d = this.getDate();
      if(d < 10) {d = "0" + d;}
      if(m < 10) {m = "0" + m;}
      return "" + y + "-" + m + "-" + d;      
    
  6. date()
    Date.prototype.date = function(){
      var yyyy = this.getFullYear().toString();
      var mm = (this.getMonth()+1).toString(); 
      var dd  = this.getDate().toString();
      return mm+'-'+dd+'-'+yyyy;
    };