要显示数据的最后一周和任何两个日期之间的数据,我正在努力

To do display the last week of data and the data between any two dates I am struggling

本文关键字:数据 之间 努力 最后一周 显示 任何两 日期      更新时间:2023-09-26

我尝试了moment.js并尝试使用jquery daypicker。但到目前为止,还没有什么真正奏效。我想当我尝试使用这些方法时,我遗漏了一些东西,或者我用错了。

正如你在下面看到的我的代码,我可以让"显示上个月的最后一个"一个基本的香草javascript。但我想让这个更好。我花了差不多一个星期的时间在这上面。我想是时候寻求帮助了。

在我的js文件中:
 'use strict';
    require('./create-data-table.scss');
    const angular = require('angular');
    // const moment = require('moment');
    const sampleApp = angular.module('sampleApp');
    sampleApp.component('createDataTable',{
      template: require('./create-data-table.html'),
      controller: 'CreateTableController',
      controllerAs:'createTableCtrl'
    });
    sampleApp.factory('listsFactory', function($q,$http){
      return {
        getLists: function(){
          return  $http.get('https://gist.githubusercontent.com/evanjacobs/c150c0375030dc4de65e9b95784dc894/raw/35c5f455b147703db3989df0cb90f5781c3b312f/usage_data.json');
        }
      };
    });
    sampleApp.controller('CreateTableController', ['$scope', '$http','listsFactory', CreateTableController]);
    function CreateTableController($scope,$http, listsFactory){
      // $scope.date ={ year:null, month:null};
      listsFactory.getLists().then(function(response){
        $scope.lists = response.data;
        console.log($scope.lists);
      }, function(error){
        console.error(error);
      });
      $scope.reverse = function() {
        $scope.lists.reverse();
      };
      $scope.sortingByMonthFilter = function(year, month) {
        console.log('filtering');
        $scope.unfilteredLists = $scope.unfilteredLists || $scope.lists;
        $scope.lists = $scope.unfilteredLists.filter((record) => {
          return record.date.includes(`${year}-${month}`);
        });
      };
      $scope.getLastWeek = function() {
        $scope.unfilteredLists = $scope.lists;
        $scope.lists = $scope.lists.filter((record) => {
          var today = new Date();
          var currentYear = today.getFullYear();
          var currentMonth = today.getMonth();
          var currentDay = today.getDate();
          var recordDate =  new Date(record.date);
          var recordYear = recordDate.getFullYear();
          var recordMonth = recordDate.getMonth();
          var recordDay = recordDate.getDate();
          var lastWeekDays = [];
          var firstDayOfLastWeek = currentDay -7;
          for(var i = currentDay; i >= firstDayOfLastWeek; i--){
            lastWeekDays.push(i);
          }
          var lastWD1 = lastWeekDays[0];
          var lastWD2 = lastWeekDays[1];
          var lastWD3 = lastWeekDays[2];
          var lastWD4 = lastWeekDays[3];
          var lastWD5 = lastWeekDays[4];
          var lastWD6 = lastWeekDays[5];
          var lastWD7 = lastWeekDays[6];
          if (recordYear === currentYear && recordMonth === currentMonth){
            console.log(recordYear,recordMonth, currentYear,currentMonth);
            // if(recordDay.matches(lastWeekDays)){
            //   return record;
            // }
            if (recordDay=== lastWD1|| lastWD2||lastWD3||   
                lastWD4||lastWD5|| lastWD6||lastWD7){
              console.log(record);
              return record;
            }
          }
          else {
            console.log('No matching data!');
          }
        });
      };
      $scope.getLastMonth = function(){
        $scope.unfilteredLists = $scope.lists;
        $scope.lists = $scope.lists.filter((record) => {
          var today = new Date();
          var currentMonth = today.getMonth();
          var currentYear = today.getFullYear();
          var recordDate =  new Date(record.date);
          var recordYear = recordDate.getFullYear();
          var recordMonth = recordDate.getMonth();
          var lastMonth = currentMonth -1;
          if (recordYear === currentYear){
            if (recordMonth === lastMonth && lastMonth !== 0 ){
              console.log(record);
              return record;
            }
            if(recordMonth === lastMonth && lastMonth === 0 ){
              recordYear = currentYear -1;
              recordMonth = 11;
              return record;
            }
          }
        });
      };
      $scope.byTwoDates = function(){
        $scope.unfilteredLists = $scope.lists;
        $scope.lists = $scope.lists.filter((record) => {
          // function daysBetween(startDate, endDate){
          //   var days = [], current = startDate;
          //   while(current <= endDate){
          //     days.push(current);
          //   }
          //   return days;
          // }
          var startD = document.getElementById('startDate').value;
          var endD = document.getElementById('endDate').value;
          console.log(startD,endD);
          var currentDate = new Date(startD);
          var d =[];
          console.log(d);
        });
      };
      $scope.sortType = 'name';
      $scope.sortReverse = false;
      $scope.searchData = '';
    }

在我的html

  <div class ="create-data-table">
      <table>
        <thead>
          <section class="bylastMonth">
            <button name="filter" ng-click="getLastMonth()">Last Month Data</button>
          </section>
          <section class="bylastWeek">
            <button name="filter" ng-click="getLastWeek()">Last Week Data</button>
          </section>
          <section class="bytwoDays">
            <!-- <input id="startDate"/>
            <input id="endDate"/> -->
            <label>Start Date</label><input id="startDate" type="date">
            <label>End Date</label><input id="endDate" type="date">
            <button name="filter" ng-click="byTwoDates()">Between Two days</button>
          </section>
          <tr>
            <td>
            <a href="#" ng-click="sortType = 'date'; sortReverse = !sortReverse">
              Date
              <span ng-show="sortType == 'date' && !sortReverse" class="fa fa-caret-down"></span>
              <span ng-show="sortType == 'date' && sortReverse" class="fa fa-caret-up"></span>
            </a>
            <section class="filteringByMonth">
              <input name="year" type="text" placeholder ="Year" ng-model="date.year" >
              <input name="month" type="text" placeholder="Month(MM)" ng-model="date.month" >
              <button name="filter" ng-click="sortingByMonthFilter(date.year, date.month)">Filter</button>
            </section>
          </td>
          <td>
            <a href="#" ng-click="sortType = 'users'; sortReverse = !sortReverse">
            Users
              <span ng-show="sortType == 'users' && !sortReverse" class="fa fa-caret-down"></span>
              <span ng-show="sortType == 'users' && sortReverse" class="fa fa-caret-up"></span>
            </a>
            <form>
              <div class="form-group">
                <div class="input-group">
                  <div class="input-group-addon">
                    <i class="fa fa-search"></i>
                  </div>
                    <input type="text"
                     name ="users"
                     class="form-control" placeholder="search by Users #" ng-model="searchData">
                </div>
              </div>
            </form>
          </td>
          <td>
            <a href="#" ng-click="sortType = 'searches'; sortReverse = !sortReverse">
            Searches
              <span ng-show="sortType == 'searches' && !sortReverse" class="fa fa-caret-down"></span>
              <span ng-show="sortType == 'searches' && sortReverse" class="fa fa-caret-up"></span>
            </a>
          </td>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="list in lists | orderBy:sortType:sortReverse | 
         filter:searchData">
          <td>{{ list.date }}</td>
          <td>{{ list.users }}</td>
          <td>{{ list.searches }}</td>
        </tr>
      </tbody>
    </table>

救我!

我假设你正在使用http://angular-ui.github.io/bootstrap/和https://github.com/urish/angular-moment,那么你需要改变你的byTwoDates函数一点,使它像

$scope.byTwoDates = function(start, end) {
    try {
      return ((moment.duration(end - start)).humanize());
    } catch (e) {
      return "It's not valid to evaluate"
    }
  };

如果你想要getLastWeek,你可以使用以下方法:

本周:

moment().startOf('isoWeek')
moment().endOf('isoWeek')
上周

moment().subtract(1, 'weeks').startOf('isoWeek')
moment().subtract(1, 'weeks').endOf('isoWeek')

查看本文档:http://momentjs.com/docs/

为例

     /*getLastWeekStart().format('DD/MM/YYYY') to get 10/09/2016 */

     function getNextWeekStart() {  // Just for demonestration if you want to work with next week you can leave this function. 
            var today = moment();
            //edited part
            var daystoMonday = 0 - (today.isoWeekday() - 1) + 7;       
            var nextMonday = today.subtract('days', daystoMonday);
            return nextMonday;
        }
        function getLastWeekStart() {
            var today = moment();
            var daystoLastMonday = 0 - (1 - today.isoWeekday()) + 7;
            var lastMonday = today.subtract('days', daystoLastMonday);
            return lastMonday;
        }
        function getLastWeekEnd() {
            var lastMonday = getLastWeekStart();
            var lastSunday = lastMonday.add('days', 6);
            return lastSunday; 
        }
    //remember you may also use this 
    moment('anydate you want ').weekday(-7).format('DD/MM/YYYY')

请随意添加任何您想要的功能,并自定义它。