I have a list of dates in column A:
Column A
2022-02-28
2022-02-28
2022-02-28
2022-02-14
2022-02-14
2022-02-07
I'm trying to write a script that counts the number of times the largest date occures. I wrote the below script
function maxcount() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var devdeploy = ss.getSheetByName("Sheet1")
var Avals = devdeploy.getRange("A2:A").getValues();
var Alength = Avals.filter(String).length;
var max = Avals[0][0]
var unique_count = 0
for (i=0; i < Alength; i++){
if (Avals[i][0] == max){
unique_count++;
}
}
Logger.log(unique_count)
}
This script works if I use integer values and have the maximum value in cell A2. However, when I use dates instead of integers it always returns a value of 1. Any ideas on why the if loop does not work on dates, but works on integers/strings? Also is there a way to improve the script to look for the maximum value in column A then find how many times it occurs?
If your date in the column "A" is the date object, how about the following modification? I thought that in your script, by var max = Avals[0][0], only 1st element is compared. And, if the values of column "A" are the date object, the date object is compared. I thought that this might be the reason for your issue.
function maxcount() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var devdeploy = ss.getSheetByName("Sheet1")
var Avals = devdeploy.getRange("A2:A" + devdeploy.getLastRow()).getValues().filter(([a]) => a.toString() != "");
var values = Avals.map(([a]) => a.getTime());
var max = Math.max(...values);
var unique_count = values.filter(v => v == max).length;
console.log(unique_count)
}