How to calculate the number of time an event occurs in any month if I have the weekly schedule?

133

If I understood your question correctly all you need is to count the number of each weekday occurrence in a given month. Here you go, in both js and 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]);
});

And here is the same thing in 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')); 
}
Share:
133
thurahtetaung
Author by

thurahtetaung

Updated on November 24, 2022

Comments

  • thurahtetaung
    thurahtetaung over 1 year

    I am currently writing a flutter app which includes displaying the weekly schedule of a class. I also have to calculate the attendance of each student. To do that, I need the number of time each subject are taught in any given month and I am stumped as I can't think of a way to do that. I have the weekly schedule of the class and I stored it in Firestore. It is formatted as below,

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

    Screenshot of a timetable entry

    where Day refers to Monday, Tuesday, ...

    It appears in the app like this.

    My problem is, to track and calculate the attendance of students, I have to know how many times each subjects are taught in a month but I dont think multiplying said times in a week by 4 is viable since days in a month are dynamic. But I don't know how to work with a calendar programmatically so I am currently out of ideas. Any hints and suggestions are appreciated. The suggestions can be in either dart or node js since I can both implement in client side or cloud functions. Thanks a lot.

    P.S - I haven't provide any code for now but please ask me for clarifications and I will provide the related codes. I just didn't want to bloat the post.

  • thurahtetaung
    thurahtetaung over 3 years
    Thank you, this is exactly what I needed.