Nodejs Week Calculate toWeekDays()

Here you can find the source of toWeekDays()

Method Source Code

Date.prototype.toWeekDays = function () {
  var weekday = this.clone()
  if (!weekday.is().sunday()) {
    weekday.sunday().add(-7).days()/* w  w w.ja va  2s . c o  m*/
  }
  var week = [weekday.clone()]
  for (var day = 0; day < 6; day++) {
    week.push(weekday.add(1).days().clone())
  }
  return week
}

Date.prototype.toNextWeekDays = function () {
  return this.add(7).days().toWeekDays()
}

Date.prototype.toPreviousWeekDays = function () {
  return this.add(-7).days().toWeekDays()
}

Date.prototype.compare = function (date) {
  var year = this.getYear() - date.getYear()
  if (year > 0)
    return "In " + year + " Years"
  else if (year < 0)
    return -year + " Years Ago"
  else {
    var month = this.getMonth() - date.getMonth()
    if (month > 0)
      return "In " + month + " Months"
    else if (month < 0)
      return -month + " Months Ago"
    else {
      var week = this.getWeekOfYear() - date.getWeekOfYear()
      if (week > 0)
        return "In " + week + " Weeks"
      else if (week < 0)
        return -week + " Weeks Ago"
      else {
        if (this.getDay() - date.getDay() == 0)
          return "Today"
        else
          return this.toString("dddd")
      }
    }
  }
}

Related

  1. getStudyWeek()
    Date.prototype.getStudyWeek = function () {
      var inc = (this.getDay() == 6) ? 1 : 0;
      return this.getWeek() + inc;
    };
    
  2. moveToBeginOfWeek(weekStart)
    Date.prototype.moveToBeginOfWeek = function(weekStart)
        if (this.getDay() != weekStart) {
            this.moveToDayOfWeek(weekStart, -1);
        return this;
    };
    
  3. moveToEndOfWeek(weekStart)
    Date.prototype.moveToEndOfWeek = function(weekStart)
        var weekEndDay = weekStart + 6;
        if (weekEndDay > 6) {
            weekEndDay -= 7;
        if (this.getDay() != weekEndDay) {
            this.moveToDayOfWeek(weekEndDay, 1);
        return this;
    };
    
  4. offsetByWeek(offset)
    Date.prototype.offsetByWeek = function(offset) {
        this.setTime(this.getTime() + offset * (7 * 24 * 60 * 60 * 1000));
        return this;
    };
    
  5. subWeeks(value)
    Date.prototype.subWeeks = function(value) {
      this.subDays(value * 7);
      return this;
    };
    
  6. toWeekEnd(startsFromSunday)
    Date.prototype.toWeekEnd = function (startsFromSunday) {
        var dayOfWeek = this.getDay();
        if (startsFromSunday) {
            return this.addDays(6 - dayOfWeek);
        if (dayOfWeek == 0) {
            dayOfWeek = 7;
        return this.addDays(7 - dayOfWeek);
    ...
    
  7. toWeekStart(startsFromSunday)
    Date.prototype.toWeekStart = function (startsFromSunday) {
        var dayOfWeek = this.getDay();
        if (startsFromSunday) {
            return this.addDays(-dayOfWeek);
        if (dayOfWeek == 0) {
            dayOfWeek = 7;
        return this.addDays(1 - dayOfWeek);
    ...
    
  8. weekNo()
    var firstMonday = new Date(1970, 0, 5).getTime()
    Date.prototype.weekNo = function() {
      var _date = new Date(this)
      _date.setHours(0,0,0,0)
      _date.setDate(_date.getDate() - (_date.getDay() || 7))
      var weekNo = Math.ceil((((_date - firstMonday) / 86400000) + 1) / 7)
      return weekNo
    Date.prototype.weekNoRelative = function() {
    ...
    
  9. weekNumber()
    Date.prototype.weekNumber = function() {
       var MaDate = this;
       var mm = MaDate.getMonth(), jj = MaDate.getDate();
       var annee = MaDate.getFullYear();
       var NumSemaine = 0,
       ListeMois = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
       if (annee %4 == 0 && annee %100 !=0 || annee %400 == 0) {ListeMois[1]=29};
       var TotalJour=0;
       for(cpt=0; cpt<mm; cpt++){TotalJour+=ListeMois[cpt];}
    ...