Date filter problem using Datatable

Asked

Viewed 228 times

0

Good afternoon,

I’m using data-Picker and I’m having trouble filtering by dates.

1 Error : The code returns me in mm/dd/YYY even with format due to line: var min = $('#min'). datepicker("getDate");

Would need it to return (dd-mm-YYYY) in format;

My js that filters:

        $.fn.dataTable.ext.search.push(
            function (settings, data, dataIndex) {
                var min = $('#min').datepicker("getDate");
                var max = $('#max').datepicker("getDate");
                console.log(max);
                var startDate = new Date(data[7]);
                if (min == null && max == null) { return true; }
                if (min == null && startDate <= max) { return true;}
                if(max == null && startDate >= min) {return true;}
                if (startDate <= max && startDate >= min) { return true; }
                return false;
            }
        );

My js of datapicker:

         $("#min").datepicker({
                onSelect: function () {
                    table.draw(); 
                },
                weekStart: 0,   
                format: 'dd/mm/yyyy',
                language: 'pt-BR',
                time: false 
            });

            $("#max").datepicker({
                format: 'dd/mm/yyyy',
                changeMonth: true,
                changeYear: true,
                onSelect: function () {
                    table.draw(); 
                },
            });

            $('#min, #max').change(function () {
                table.draw();
            });

My js from

         var table = "";
            table = $('#aportes-table').DataTable({
                "language" : {
                    "sEmptyTable": "Nenhum registro encontrado",
                    "sInfo": "Mostrando _START_ até _END_ de _TOTAL_ Resultados",
                    "sInfoEmpty": "Mostrando 0 até 0 de 0 registros",
                    "sInfoFiltered": "(Filtrados de MAX registros)",
                    "sInfoPostFix": "",
                    "sInfoThousands": ".",
                    "sLengthMenu": "_MENU_ resultados por página",
                    "sLoadingRecords": "Carregando...",
                    "sProcessing": "Processando...",
                    "sZeroRecords": "Nenhum registro encontrado",
                    "sSearch": "Pesquisar",
                    "oPaginate": {
                        "sNext": "Próximo",
                        "sPrevious": "Anterior",
                        "sFirst": "Primeiro",
                        "sLast": "Último"
                    },
                    "oAria": {
                        "sSortAscending": ": Ordenar colunas de forma ascendente",
                        "sSortDescending": ": Ordenar colunas de forma descendente"
                    }
                },
                dom: 'Bfrtip',  
                buttons: [
                    'copyHtml5',
                    'excelHtml5',
                    'csvHtml5',
                ],
            });
  • have you tried it? #Xa;var min = $.datepicker.formatDate("dd-mm-YYYY", $("#min").datepicker("getDate"));

1 answer

0

Try to use

dateFormat: 'dd/mm/y'

Example:

$( ".startdate" ).datepicker({
  dateFormat: 'dd/mm/y',//check change
  changeMonth: true,
  changeYear: true
});
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/js/bootstrap.js" data-modules="effect effect-bounce effect-blind effect-bounce effect-clip effect-drop effect-fold effect-slide"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />


 <h1>Datepicker with Date Format (dd/mm/y) using JQuery</h1>
  <p>Date picker: <input type="text" class="startdate" size="30"/></p>
  <br> 
  Date Format : dd/mm/y

You tend better here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.