首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取日期计数

获取日期计数
EN

Stack Overflow用户
提问于 2015-06-01 02:19:04
回答 1查看 34关注 0票数 0

我想使用angularJS来计算一个月内一周的天数。例如,获取一个月中星期六的计数。我怎么才能得到它呢?谢谢以下是我的代码:

代码语言:javascript
复制
<!doctype html>
<html ng-app="myApp">
<head>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
</head>
<body>

<div ng-app="myApp" ng-controller="myCtrl" ng-init="getdates()">
   <p ng-if="x='Sat'">{{dates.length}}</p>
   <div ng-repeat="x in dates">
    <input type="checkbox" ><p ng-show="x">{{x | format:'ddd'}}</p></input>
  </div>

</div>
  <script>



angular.module('myApp', [])
.controller('myCtrl', function($scope) {
  $scope.dates=[];
  $scope.getdates=function(){
    for(i=0;i<31;i++){
      $scope.myDate = i+' Feb 2015 00:00:00 GMT';
      $scope.dates.push($scope.myDate)
    }
  };

})
.filter('format', function() {
  return function(input, format) {
    return moment(new Date(input)).format(format);
  };
})
;
  </script>

</body>
</html>
EN

回答 1

Stack Overflow用户

发布于 2015-06-01 02:48:12

通过以下方式:

GET/INIT:

  • 获取日期作为参数
  • 获取输入的年份和月份(yearVar,monthVar)
  • 获取该月的最后一天(last_day_of_month)

然后

  • 从第一天开始var currDate = new Date(yearVar, monthVar, 1)

查找所需日期的第一个匹配项(6为if(currDate.getDay() == 6) )

如果当天小于last_day_of_month,请记住此值,如果

  • 小于last_day_of_month

,则为multiple

DATE API

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30560957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档