Reputation: 107
I'm implementing a date range filter in datatables, when displaying the column date in format YYYY-MM-DD , the filter works perfectly, but I need to display the column in DD-MM-YYYY format , so i applied moment .
callback function using moment in datatable:
{ targets : [8],
render : function (data, type, row) {
return moment(data).format('DD/MM/YYYY')
}
},
As a result I see the next day displayed on the columns. image below. I would like to filter by date and the columns be ordered in the following format DD/MM/YYYY
clearly i'm selecting day 10 in datepicker filter and it displays day 11 on the table.
$(document).ready(function(){
$.fn.dataTable.ext.search.push(
function (settings, data, dataIndex) {
var fecha_inicio = $('#fecha_inicio').datepicker("getDate");
// alert(fecha_inicio);
var fecha_fin = $('#fecha_fin').datepicker("getDate");
var arr = data[8].split('/').reverse().join('-');
// alert(arr);
// var temp = arr[0] +'-'+ arr[1] +'-'+arr[2];
var startDate = new Date(arr);
startDate.setHours(0,0,0,0);
// alert(startDate);
if (fecha_inicio == null && fecha_fin == null) { return true;}
if (fecha_inicio == null && startDate <= fecha_fin) { return true;}
if(fecha_fin == null && startDate >= fecha_inicio) { return true;}
if (startDate <= fecha_fin && startDate >= fecha_inicio) { return true;}
return false;
}
);
If i take out the callback code the column display correctly, except the date format.
Thanks in advance.
Upvotes: 1
Views: 3854
Reputation: 188
maybe you want to use type
parameter,for example
render:function(data,type,row,meta){
if(type=='display'){
return moment(data).format('DD/MM/YYYY');
}
if(type=='filter'){
//there is an example, show how to work, you can use whatever you want time format
return moment(data).format('YYYY/MM/DD');
}
if(type=='sort'){
//maybe there is timestamp format
return data;
}
return data;
}
for more details check https://datatables.net/reference/option/columns.render function render( data, type, row, meta )
part of
Upvotes: 1