Nodejs Week Calculate weekNumber()

Here you can find the source of weekNumber()

Method Source Code

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];}
   TotalJour+=jj;/*from  ww w.ja  va  2s .  c o  m*/
   DebutAn = new Date(annee,0,1);
   var JourDebutAn;
   JourDebutAn=DebutAn.getDay();
   if(JourDebutAn==0){JourDebutAn=7};

   TotalJour-=8-JourDebutAn;
   NumSemaine = 1;
   NumSemaine+=Math.floor(TotalJour/7);
   if(TotalJour%7!=0){NumSemaine+=1};

   return(NumSemaine);
}

Related

  1. subWeeks(value)
    Date.prototype.subWeeks = function(value) {
      this.subDays(value * 7);
      return this;
    };
    
  2. toWeekDays()
    Date.prototype.toWeekDays = function () {
      var weekday = this.clone()
      if (!weekday.is().sunday()) {
        weekday.sunday().add(-7).days()
      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")
    
  3. 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);
    ...
    
  4. 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);
    ...
    
  5. 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() {
    ...
    
  6. weekday()
    Date.prototype.weekday = function()
      var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
      return days[this.getDay()];