I'm trying to develop a custom date range filter for a dataTables. The user can enter a start date and an end date in two inputs and filter the whole table.
When entering a beginning and end date, the results match. When I enter only one of the two, the results fail. For example; the start date is 12/01/2017 but I can see instances with the date 29-11-2016... My code for the filtering:
$('#filter').on('click', function(e) {
e.preventDefault();
var startDate = $('#start').val(),
endDate = $('#end').val();
filterByDate(10, startDate, endDate); // We call our filter function
$dTable.dataTable().fnDraw(); // Manually redraw the table after filtering
});
var filterByDate = function(column, startDate, endDate) {
$.fn.dataTableExt.afnFiltering.push(
function( oSettings, aData, iDataIndex ) {
var rowDate = normalizeDate(aData[10]),
start = normalizeDate(startDate),
end = normalizeDate(endDate);
// If our date from the row is between the start and end
if (start <= rowDate && rowDate <= end) {
console.log(start <= rowDate);
return true;
} else if (rowDate >= start && end === '' && start !== ''){
console.log(rowDate >= start && end === '' && start !== '');
return true;
} else if (rowDate <= end && start === '' && end !== ''){
return true;
} else {
return false;
}
}
);
};
var normalizeDate = function(dateString) {
var date = new Date(dateString);
var normalized = ("0" + date.getDate()).slice(-2) + '/'+ (("0" + (date.getMonth() + 1)).slice(-2)) + '/' +date.getFullYear() ;
return normalized;
};
Could some JavaScript genius help me to get the comparison of rowData >= start....
and rowDate <= end....
working correctly? The input dates are separated by /
and in my table by /
, but the normalizeData function makes them the same layout.