我怎样才能获得下一个和前一个日期的所有日子

时间:2017-07-05 06:01:33

标签: javascript jquery angularjs date datetime

我希望获得下一个和上一个日期的所有日子。 我有三个按钮,有PrevDate,TodayDate,NextDate。 当我点击prevDate按钮上的第一次时,它应该显示上一个日期并点击它显示前一天的第二次类似它应该显示我点击的所有上一个日期..同样在Nextdate中也显示第二天的日期。第二次点击它显示日期的日期.. 这是我的HTML代码:

<body ng-app="app" ng-controller="myCtrl">
 <div class="col-sm-10" >   
   <button style="font-size:14px" ng-click="prevBtn()">Previous day</button>
   <button style="font-size:14px" ng-click="todayBtn()" >&nbsp;Today&nbsp;</button>
   <button style="font-size:14px" ng-click="nextBtn()" >Next day</button>
   {{date}}
 </div>
</body>

app.js代码:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
   var today = new Date();

       $scope.prevBtn = function(){
           var yesterday = new Date(today);
           yesterday.setDate(today.getDate() - 1);
           var dd = yesterday.getDate();
           var mm = yesterday.getMonth()+1; 
           var yyyy = yesterday.getFullYear();
           yesterday = dd+'-'+mm+'-'+yyyy;  
           console.log(yesterday);
           $scope.date = yesterday;
       }
});

此代码仅显示上一个日期.. 我希望获得Prev日期的所有日子.... 请帮助我...谢谢......

1 个答案:

答案 0 :(得分:1)

保留当前日期的参考

mycomp

以下代码段可以为您的示例应用提供帮助

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
  var today = new Date();

  var curDay = new Date(today);
  $scope.prevBtn = function() {

    curDay.setDate(curDay.getDate() - 1);
    var dd = curDay.getDate();
    var mm = curDay.getMonth() + 1;
    var yyyy = curDay.getFullYear();
    var yesterday = dd + '-' + mm + '-' + yyyy;
    console.log(yesterday);
    $scope.date = yesterday;
  }
});
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
  var today = new Date();

  var curDay = new Date(today);
  $scope.prevBtn = function() {

    curDay.setDate(curDay.getDate() - 1);
    showDate();

  }

  $scope.todayBtn = function() {
    curDay = new Date(today);
    showDate();
  };

  $scope.nextBtn = function() {
    curDay.setDate(curDay.getDate() + 1);
    showDate();
  };

  function showDate() {
    var dd = curDay.getDate();
    var mm = curDay.getMonth() + 1;
    var yyyy = curDay.getFullYear();
    var dateStr = dd + '-' + mm + '-' + yyyy;
    console.log(dateStr);
    $scope.date = dateStr;
  };

});