Nodejs Day Calculate subDays(value)

Here you can find the source of subDays(value)

Method Source Code

Date.parseDate = function (date, format) {
  if (format === undefined)
    format = 'Y-mm-dd';
  return new Date(moment(date, format).valueOf());
};

Date.prototype.subDays = function (value) {
  this.setDate(this.getDate() - value);//from   ww w  .  jav  a  2  s .c om
  return this;
};

Date.prototype.subMonths = function (value) {
  var date = this.getDate();
  this.setMonth(this.getMonth() - value);
  if (this.getDate() < date) {
    this.setDate(0);
  }
  return this;
};

Date.prototype.subWeeks = function (value) {
  this.subDays(value * 7);
  return this;
};

Date.prototype.subYears = function (value) {
  var month = this.getMonth();
  this.setFullYear(this.getFullYear() - value);
  if (month < this.getMonth()) {
    this.setDate(0);
  }
  return this;
};

Date.prototype.format = function (format) {
  if (format === undefined)
    return this.toString();
  return moment(this).format(format);
};

Related

  1. past(pattern,pastDays)
    Date.prototype.past = function(pattern,pastDays){
        var pastday = new Date((this - 0) - 1000*60*60*24*pastDays);
        var pattern = pattern;    
        var dateObj = {
            "Y" : pastday.getFullYear(),
            "M" : pastday.getMonth()+1,
            "D" : pastday.getDate(),
            "h" : pastday.getHours(),
            "m" : pastday.getMinutes(),
    ...
    
  2. relativeDays()
    Date.prototype.relativeDays = function() {
      var now = new Date();
      var interval = now.getTime() - this.getTime();
      interval = Math.floor(interval / (1000 * 60 * 60 * 24));
      return interval;
    };
    
  3. sGetDay()
    var weekdays = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"];
    Date.prototype.sGetDay = function() {
      return (this.getDay() + 6) %7;
    var d = new Date();
    var today = weekdays[d.sGetDay()];
    
  4. setToMondayMidnight()
    Date.prototype.setToMondayMidnight = function() {
        var weekDay = this.getDay();
        if (weekDay === 0) { 
            weekDay = 7;
        this.setDate(this.getDate() - weekDay + 1);
        this.timeToMidnight();
    };
    
  5. shiftDays(days)
    Date.prototype.shiftDays = function(days){    
      days = parseInt(days, 10);
      this.setDate(this.getDate() + days);
      return this;
    $date = new Date(2014, 9, 16,0,1);
    $date.shiftDays(1);
    console.log($date-"");
    $date.shiftDays(1);
    ...
    
  6. subDays(value)
    Date.prototype.subDays = function(value) {
      this.setDate(this.getDate() - value);
      return this;
    };
    
  7. subtract(days)
    const times = [1000, 10000, 100000, 1000000, 5000000];
    let x = 0;
    Date.prototype.subtract = function(days) {
      return new Date(this.setDate(this.getDate() - days));
    for(i in times) {
      console.time('monkeypatch ' + times[i]);
      while (x < times[i]) {
        x++;
    ...
    
  8. subtractDays(days)
    Date.prototype.subtractDays=function(days){
        if (arguments.length===1){
            if (typeof days !== 'number') return;
            this.setDate(this.getDate() - days);
            return this;
        return this;
    
  9. yestoday(pattern)
    Date.prototype.yestoday = function(pattern){
        return this.past(pattern,1);
    };