Moment.JS Get The Sunday Before A Given Date
<p>I'm modifying Angular's UI Bootstrap Datepicker to allow for a week selection mode. In order for this to work effectively, I need to set the <code>activeDate</code> to the first Sunday of a given week, so if you select a date, say a Monday like 8/29/2016, it converts your selection into the earlier Sunday, 8/28/16.</p>

<p>This is what the selection function looks like. If interested in the conversion to Sunday logic in the <code>else if</code> block where the alert matches.</p>

<pre><code>$scope.select = function(date) {
    if ($scope.datepickerMode === self.minMode) {
      var dt = ngModelCtrl.$viewValue ? dateParser.fromTimezone(new Date(ngModelCtrl.$viewValue), ngModelOptions.timezone) : new Date(0, 0, 0, 0, 0, 0, 0);
      dt.setFullYear(date.getFullYear(), date.getMonth(), date.getDate());
      dt = dateParser.toTimezone(dt, ngModelOptions.timezone);
      ngModelCtrl.$setViewValue(dt);
      ngModelCtrl.$render();
    } 

    else if($scope.datepickerMode =="week")
      {
        alert("Match!");
        self.activeDate = date.firstSundayofthatweek() //what do I actually put here using Moment.js?
       setMode(self.modes[self.modes.indexOf($scope.datepickerMode) - 1]);
      $scope.$emit('uib:datepicker.mode');
      }

    else {
      self.activeDate = date;
      setMode(self.modes[self.modes.indexOf($scope.datepickerMode) - 1]);
      $scope.$emit('uib:datepicker.mode');
    }

    $scope.$broadcast('uib:datepicker.focus');
  };
</code></pre>

<p>I'm using moment.js but can't figure out in their docs how to do it. A vanilla JS answer would also be appreciated. <code>date</code> that is passed to the function is a date object.</p>

<p>Moment docs suggest either:</p>

<p><code>moment().day(-7);</code></p>

<p>or</p>

<p><code>moment().weekday(-7);</code></p>

<p>but I am passing a date object, not a moment... so do I convert or chain or something? Sorry a bit confused... I also need to return a date object to <code>self.activeDate</code></p>