Search code examples
angularjslistboxangular-ui-datepicker

List all saturdays and sundays


How should I achieve listing all saturdays and sundays between two dates into a listbox or textarea in Angular JS date picker on a button click and save them on another screen as Weekend under holiday name and the list of dates under holiday date? I'm trying to display all the holidays list of records with column holiday name as weekend and dates for all the saturdays and sundays as below. I will be entering the holiday name in the text box and click on the Weekend button to get all the dates of saturdays and sundays and save changes to store the multiple list of records.

Holiday Name Holiday Date

Weekend 25-07-2015

Weekend 26-07-2015

...


Solution

  • Getting the weekends is relatively straightforward using a while loop and incrementing the active day:

      $scope.start=new Date(2015,6,1);
      $scope.end= new Date(2015,8,1);
      $scope.weekends = [];
    
      var day = angular.copy($scope.start);
      while(day < $scope.end){        
         var d = day.getDay(); 
         if(d === 0 || d === 6){
           $scope.weekends.push(new Date(day));
         }
         day.setDate(day.getDate()+1);
      }
    

    Change to <= if you want to include last day.

    You would need a list of holidays and their dates for whatever locale the user is in to generate that list from within the same loop

    DEMO