2016-08-09 239 views
0

我最近用moment.js實現了一個日期選擇器。由於某種原因它顯示錯誤的日期。我在日期選擇器之外顯示了日期,這很好。我console.logged變量,看看是什麼被存儲,結果是我想要的格式。但是,當我在日期選擇器中調用數據時,它將顯示錯誤的格式。它應該和日期選擇器下面輸出的完全一樣。有人能解釋我要去哪裏嗎?日期選擇器顯示在2個不同的日期

日期選擇器和正確的日期被下輸出:

enter image description here

<div class="datepicker-container"> 
     <div class="date-from"> 
      From: 
      <datepicker date-set="{{yesterday}}" selector="form-control" date-max-limit="{{today}}" 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-set="{{today}}" selector="form-control" date-min-limit="{{yesterday}}" 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> 
     <h4>{{yesterday}} and {{today}}</h4> 

定義日期:

enter image description here

var currentDate = moment(new Date()).format("DD/MM/YYYY"); 
console.log("1", currentDate); 

$scope.today = currentDate.toString(); 
console.log("2", $scope.today); 

var yesterdaysDate = moment(new Date()).subtract(1, 'days').format("DD/MM/YYYY"); 
console.log("3", yesterdaysDate); 

$scope.yesterday = yesterdaysDate.toString(); 
console.log("4", $scope.yesterday); 
+1

你能提供'datepicker'指令/組件聲明? –

回答

0

解決了它,我用日期選擇器格式化了日期,而不是片刻。

日期選取器:

<div class="datepicker-container"> 
    <div class="date-from"> 
     From: 
     <datepicker date-set="{{yesterday}}" date-format="dd/MM/yyyy" selector="form-control" date-max-limit="{{today}}" 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-set="{{today}}" date-format="dd/MM/yyyy" selector="form-control" date-min-limit="{{yesterday}}" 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> 
    <h4>{{yesterday}} and {{today}}</h4> 

時刻:

var currentDate = moment(new Date()); 
console.log("1", currentDate); 

$scope.today = currentDate.toString(); 
console.log("2", $scope.today); 

var yesterdaysDate = moment(new Date()).subtract(1, 'days'); 
console.log("3", yesterdaysDate); 

$scope.yesterday = yesterdaysDate.toString(); 
console.log("4", $scope.yesterday); 
相關問題