Aggregate by date

Hi guys:

I have an aggregation for my database but i need to group by date interval. This is the code:

export function datePicker2_change_1(event)  {

function pad(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}
const pickerDay = $w('#datePicker1').value.getDate().toLocaleString();
const pickerMonth = $w('#datePicker1').value.getMonth()+1;
const pickerYear = $w('#datePicker1').value.getFullYear().toString();
const pickerDateMin = new Date(pickerYear+"-"+pad(pickerMonth,2)+"-"+pad(pickerDay,2)+"T00:00:00.000Z");
const pickerDay2 = $w('#datePicker2').value.getDate().toLocaleString();
const pickerMonth2 = $w('#datePicker2').value.getMonth()+1;
const pickerYear2 = $w('#datePicker2').value.getFullYear().toString();
const pickerDateMax = new Date(pickerYear2+"-"+pad(pickerMonth2,2)+"-"+pad(pickerDay2,2)+"T23:59:59.999Z");
console.log(pickerDateMin, "end date"); 
console.log(pickerDateMax, "end date");
const filter = wixData.filter().contains("codEmpresa", codEmpresa)
const dates = wixData.filter().gt("fechaConsulta", pickerDateMin).lt("fechaConsulta", pickerDateMax);

wixData.aggregate("HistoriaClinica")
  .having(dates)
  .filter(filter)
  .group("ciudad")
  .count()
  .run()
  .then( (results) => {
 let items = results.items;
 
    $w("#table1").rows = items;
    console.log(items)
})

}

But it doesnt work. Any suggestions?