0

I've been trying out this AngularJS package (https://www.npmjs.com/package/angularjs-datepicker) and was struggling to use the date-set property. In my example I have 2 calendars, 1 that should display yesterdays date and 1 that should display todays date. Could someone please help me set these dates?

Datepicker:

  <div class="datepicker-container">
    <div class="date-from">
      From:
      <datepicker date-format="dd/MM/yyyy" selector="form-control" date-set="{{}}" class="date-picker">
        <div class="input-group">
          <input class="form-control" placeholder="Choose a date"/>
          <span class="input-group-addon" style="cursor: pointer">
            <i class="glyphicon glyphicon-calendar"></i>
          </span>
        </div>
      </datepicker>
    </div>
    <div class="date-too">
      To:
      <datepicker date-format="dd/MM/yyyy" selector="form-control" date-set="{{}}" class="date-picker">
        <div class="input-group">
          <input class="form-control" placeholder="Choose a date"/>
          <span class="input-group-addon" style="cursor: pointer">
            <i class="glyphicon glyphicon-calendar"></i>
          </span>
        </div>
      </datepicker>
    </div>
  </div>

Setting the date:

$scope.dateFrom = new Date();
$scope.dateTo = new Date();

3 Answers 3

3
  var date = new Date();

  $scope.today = date; //to show today's date

  date.setDate(date.getDate() - 1); //to get yesterday's date, Ref:http://stackoverflow.com/questions/5511323/javascript-yesterday

  $scope.yesterday = date;

Using date-set:

From the documentation linked above, https://www.npmjs.com/package/angularjs-datepicker . I see you have to pass date object as string to attribute date-set , hence convert the date object to string.

//js 
var date = new Date();

$scope.today = date.toString(); //to show today's date

date.setDate(date.getDate() - 1); //to get yesterday's date, Ref:http://stackoverflow.com/questions/5511323/javascript-yesterday

$scope.yesterday = date.toString();



//html
<datepicker date-format="dd/MM/yyyy" selector="form-control" date-set="{{today}}" class="date-picker">

<datepicker date-format="dd/MM/yyyy" selector="form-control" date-set="{{yesterday}}" class="date-picker">
Sign up to request clarification or add additional context in comments.

1 Comment

How would I then use that in the 'date-set={{}}'?
0

see this plnkr http://plnkr.co/edit/mDHliPweKoUNOAmVv5oo?p=preview

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<pre>Raw date is: <em>{{dt}}</em></pre>

Comments

0

I solved it in a simpler way, just activated the ng-if attribute in the div before the datepicker

 pagamentoService.getCronograma(moment().year()).then(function (resp){
                
            var cronogramas = resp.data;
            console.log('99',resp.data);
            
            cronogramas.forEach(function (obj){
                vm.datasBloqueadas.push(moment(obj.data_limite).format('YYYY-M-D'));
            });
           
        }).catch(function (err){
            console.log(err);
        });
<div class="col-md-4 mt-1">
                    <label>data vencimento</label>
                    <div ng-if="Beneficios.datasBloqueadas.length > 0" class="input-control full-size text">
                        <datepicker date-format="yyyy-MM-dd" date-disabled-dates="{{Beneficios.datasBloqueadas}}" >
                            <input id="dataVencimento"  class="required" type="date" date-converter ng-model="Beneficios.view.data_filter"
                            data-role="hint" data-hint-background="bg-orang-e"
                            data-hint-color="fg-white" data-hint-position="bottom" data-hint-mode="2">
                            <button class="button" onclick="$('#dataVencimento').focus();">
                                <span class="mif-calendar"></span>
                            </button>
                        </datepicker>
                    </div>

1 Comment

Hi Weinny and thanks for the contribution! Could you edit your answer to include a basic explanation of the solution you suggested (i.e. what is ng-if and what it does)? Then your answer will serve future readers even better. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.