Reputation: 2272
So, i am storing all the dates in a list and i want to check how many dates are not present so i can get the ratio for attendance.
My Code:-
List<String> dates=[];//have some dates assigned to it
// now suppose user was absent on a date x and y then i want to calculate the attendance as:-
attendance=(present/total)*100;
Upvotes: 1
Views: 369
Reputation: 719
var firstDate=new DateTime(2021,01,04);
var curDate=new DateTime.now();
var days=curDate.difference(firstDate).inDays;
print(days);
List<String> allDates=List.generate(days,(index)=>firstDate.add(Duration(days:index)).toString().substring(0,10).replaceAll("-",".")).toList();
List<String> userDates=[
"2021.04.01",
"2021.04.02",
"2021.04.03",
"2021.04.04",
"2021.04.05",
"2021.04.06",
];
// non-user dates
var d=allDates.where((d)=>!userDates.contains(d)).toList();
print(d);
var attendance=(d.length/allDates.length)*100;
print(attendance);
Outputs: [2021.01.04, 2021.01.05, 2021.01.06, 2021.01.07, 2021.01.08, 2021.01.09, 2021.01.10, 2021.01.11, 2021.01.12, 2021.01.13, 2021.01.14, 2021.01.15, 2021.01.16, 2021.01.17, 2021.01.18, 2021.01.19, 2021.01.20, 2021.01.21, 2021.01.22, 2021.01.23, 2021.01.24, 2021.01.25, 2021.01.26, 2021.01.27, 2021.01.28, 2021.01.29, 2021.01.30, 2021.01.31, 2021.02.01, 2021.02.02, 2021.02.03, 2021.02.04, 2021.02.05, 2021.02.06, 2021.02.07, 2021.02.08, 2021.02.09, 2021.02.10, 2021.02.11, 2021.02.12, 2021.02.13, 2021.02.14, 2021.02.15, 2021.02.16, 2021.02.17, 2021.02.18, 2021.02.19, 2021.02.20, 2021.02.21, 2021.02.22, 2021.02.23, 2021.02.24, 2021.02.25, 2021.02.26, 2021.02.27, 2021.02.28, 2021.03.01, 2021.03.02, 2021.03.03, 2021.03.04, 2021.03.05, 2021.03.06, 2021.03.07, 2021.03.08, 2021.03.09, 2021.03.10, 2021.03.11, 2021.03.12, 2021.03.13, 2021.03.14, 2021.03.15, 2021.03.16, 2021.03.17, 2021.03.18, 2021.03.19, 2021.03.20, 2021.03.21, 2021.03.22, 2021.03.23, 2021.03.24, 2021.03.25, 2021.03.26, 2021.03.27, 2021.03.28, 2021.03.29, 2021.03.30, 2021.03.31, 2021.04.07, 2021.04.08]
Upvotes: 1