AngularJS datepicker使用日期设置

时间:2016-08-09 08:58:25

标签: javascript angularjs date datepicker

我一直在试用这个AngularJS包(https://www.npmjs.com/package/angularjs-datepicker),并且正在努力使用date-set属性。在我的示例中,我有2个日历,1个应该显示昨天的日期,1个应该显示今天的日期。有人可以帮我设定这些日期吗?

日期选择器:

  <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>

设置日期:

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

3 个答案:

答案 0 :(得分:2)

  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;

使用日期设置:

从上面链接的文档中https://www.npmjs.com/package/angularjs-datepicker。我看到你必须将date对象作为string传递给属性date-set,因此将日期对象转换为字符串。

//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">

答案 1 :(得分:0)

请参阅此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>

答案 2 :(得分:0)

我以一种更简单的方式解决了它,只是在datepicker之前在div中激活了ng-if属性

 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>