【问题标题】:How to calculate the number of time an event occurs in any month if I have the weekly schedule?如果我有每周计划,如何计算事件在任何月份发生的次数?
【发布时间】:2020-12-05 19:17:47
【问题描述】:

我目前正在编写一个颤振应用程序,其中包括显示每周的课程安排。我还必须计算每个学生的出勤率。要做到这一点,我需要每个科目在任何给定月份的教学时间,我很困惑,因为我想不出办法来做到这一点。 我有每周的课程表,并将其存储在 Firestore 中。格式如下,

{Day: 1 , Major: 'EcE', Subject: 'Communication', Year: 5, Period: 1, ...}

Screenshot of a timetable entry

Day 指的是星期一、星期二……

它出现在应用程序中,如this

我的问题是,要跟踪和计算学生的出勤率,我必须知道每个科目在一个月内教授了多少次,但我认为将所说的一周内的次数乘以 4 是不可行的,因为一个月中的天数是动态的。但我不知道如何以编程方式使用日历,所以我目前没有想法。任何提示和建议表示赞赏。建议可以在 dart 或 node js 中,因为我可以在客户端或云功能中实现。非常感谢。

P.S - 我暂时没有提供任何代码,但请向我澄清,我将提供相关代码。我只是不想让帖子膨胀。

【问题讨论】:

    标签: algorithm flutter dart


    【解决方案1】:

    如果我正确理解了您的问题,您只需计算给定月份中每个工作日发生的次数。 在这里,在 js 和 dart 中:

    JS:

    var dtNow = new Date(); // replace this for the month/date of your choosing
    var dtFirst = new Date(dtNow.getFullYear(), dtNow.getMonth(), 1);  // first date in a month
    var dtLast = new Date(dtNow.getFullYear(), dtNow.getMonth() + 1, 0); // last date in a month
    
    // we need to keep track of weekday occurrence in a month, a map looks suitable for this
    var dayOccurrence = {
      "Monday" : 0, 
      "Tuesday" : 0, 
      "Wednesday" : 0, 
      "Thursday" : 0, 
      "Friday" : 0, 
      "Saturday" : 0, 
      "Sunday" : 0  
    }
    
    var dtLoop = new Date(dtFirst); // temporary date, used for looping
    while(dtLoop <= dtLast){
      //  getDay() method returns the day of the week, from 0(Sunday) to 6(Saturday) 
      switch(dtLoop.getDay()) {
        case 0:
          dayOccurrence.Sunday++; break;
        case 1:
          dayOccurrence.Monday++; break;
        case 2:
          dayOccurrence.Tuesday++; break;
        case 3:
          dayOccurrence.Wednesday++; break;
        case 4:
          dayOccurrence.Thursday++; break;
        case 5:
          dayOccurrence.Friday++; break;
        case 6:
          dayOccurrence.Saturday++; break; 
        default:
          console.log("this should not happen");
       }
       dtLoop.setDate(dtLoop.getDate() + 1);
    }
    
    // log the results
    var keys = Object.keys(dayOccurrence);
    keys.forEach(key=>{
      console.log(key + ' : ' + dayOccurrence[key]);
    });

    在 dart 中也是一样的:

    void main() {
      DateTime dtNow = new DateTime.now(); // replace this for the month/date of your choosing
      DateTime dtFirst = new DateTime(dtNow.year, dtNow.month, 1);  // first date in a month
      DateTime dtLast = new DateTime(dtNow.year, dtNow.month + 1, 0); // last date in a month
      
      // we need to keep track of weekday occurrence in a month, a map looks suitable for this
      Map<String, int> dayOccurrence = {
      'Monday' : 0, 
      'Tuesday' : 0, 
      'Wednesday' : 0, 
      'Thursday' : 0, 
      'Friday' : 0, 
      'Saturday' : 0, 
      'Sunday' : 0  
      };
      
      DateTime dtLoop = DateTime(dtFirst.year, dtFirst.month, dtFirst.day);
      while(DateTime(dtLoop.year, dtLoop.month, dtLoop.day) != dtLast.add(new Duration(days: 1))){
        // weekday is the day of the week, from 1(Monday) to 7(Sunday) 
        switch(dtLoop.weekday) {
        case 1:
          dayOccurrence['Monday']++; break;
        case 2:
          dayOccurrence['Tuesday']++; break;
        case 3:
          dayOccurrence['Wednesday']++; break;
        case 4:
          dayOccurrence['Thursday']++; break;
        case 5:
          dayOccurrence['Friday']++; break;
        case 6:
          dayOccurrence['Saturday']++; break;
        case 7:
          dayOccurrence['Sunday']++; break;
        default:
          print("this should not happen");
        } 
        dtLoop = dtLoop.add(new Duration(days: 1));
      }
      
      // log the results
      dayOccurrence.forEach((k,v) => print('$k : $v')); 
    }
    

    【讨论】:

    • 谢谢,这正是我需要的。
    猜你喜欢
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    相关资源
    最近更新 更多