Data Tables Date Range filter

With Custom & Range filter

by Msk_Satheesh

HTML

<script src="http://cdn.datatables.net/1.10.0/js/jquery.dataTables.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="http:////maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css">
<script src="http://eternicode.github.io/bootstrap-datepicker/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>
<link rel="stylesheet" href="http://eternicode.github.io/bootstrap-datepicker/bootstrap-datepicker/css/datepicker3.css">
<div id="dt_example">
    <br>
    <br>
    <table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
        <thead>
            <tr>
                <th>Header Col1</th>
                <th>Header Col2</th>
                <th>Header Col3</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Value11</td>
                <td>Value21</td>
                <td>01/11/2014</td>
            </tr>
            <tr>
                <td>Value12</td>
                <td>Value22</td>
                <td>01/14/2014</td>
            </tr>
             <tr>
                <td>Value12</td>
                <td>Value22</td>
                <td>01/31/2014</td>
            </tr>
            <tr>
                <td>Value12</td>
                <td>Value22</td>
                <td>12/31/2014</td>
            </tr>
             <tr>
                <td>Value12</td>
                <td>Value22</td>
                <td>02/01/2014</td>
            </tr>
            <tr>
                <td>Value11</td>
                <td>Value21</td>
                <td>07/11/2014</td>
            </tr>
            <tr>
                <td>Value12</td>
                <td>Value22</td>
                <td>07/14/2014</td>
            </tr>
             <tr>
                <td>Value12</td>
                <td>Value22</td>
           ...

CSS

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * General page setup
 */
 #dt_example {
    font: 80%/1.45em"Lucida Grande", Verdana, Arial, Helvetica, sans-serif;
    margin: 0;
    padding: 0;
    color: #333;
    background-color: #fff;
}
#dt_example #container {
    width: 800px;
    margin: 30px auto;
    padding: 0;
}
#dt_example #footer {
    margin: 50px auto 0 auto;
    padding: 0;
}
#dt_example #demo {
    margin: 30px auto 0 auto;
}
#dt_example .demo_jui {
    margin: 30px auto 0 auto;
}
#dt_example .big {
    font-size: 1.3em;
    font-weight: bold;
    line-height: 1.6em;
    color: #4E6CA3;
}
#dt_example .spacer {
    height: 20px;
    clear: both;
}
#dt_example .clear {
    clear: both;
}
#dt_example pre {
    padding: 15px;
    background-color: #F5F5F5;
    border: 1px solid #CCCCCC;
}
#dt_example h1 {
    margin-top: 2em;
    font-size: 1.3em;
    font-weight: normal;
    line-height: 1.6em;
    color: #4E6CA3;
    border-bottom: 1px solid #B0BED9;
    clear: both;
}
#dt_example h2 {
    font-size: 1.2em;
    font-weight: normal;
    line-height: 1.6em;
    color: #4E6CA3;
    clear: both;
}
#dt_example a {
    color: #0063DC;
    text-decoration: none;
}
#dt_example a:hover {
    text-decoration: underline;
}
#dt_example ul {
    color: #4E6CA3;
}
.css_right {
    float: right;
}
.css_left {
    float: left;
}
.demo_links {
    float: left;
    width: 50%;
    margin-bottom: 1em;
}
#demo_info {
    padding: 5px;
    border: 1px solid #B0BED9;
    height: 100px;
    width: 100%;
    overflow: auto;
}
#dt_example code {
    font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
    padding: 2px 4px !important;
    white-space: nowrap;
    font-size: 0.9em;
    color: #D14;
    background-color: #F7F7F9;
    border: 1px solid #E1E1E8;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;
}
/*
 * Table
 */
 table.dataTable {
    margin: 0 auto;
    clear: both;
    width:...

JavaScript

$(document).ready(function () {
    $('#example').dataTable({
        "sDom": '<lf<t>ip>',
            "bScrollInfinite": false,
            "bScrollCollapse": false,
            "sScrollY": "150px"
    });
});

$(function () {
    $("div .divDatePicker").each(function () {
        $(this).datepicker().on('changeDate', function (ev) {
            $(this).datepicker("hide");
        });
    });
    $(document).on('change', '#fromDate, #toDate', function () {
        $('#example').dataTable().DataTable().draw();
    });

});


$.fn.dataTableExt.afnFiltering.push(


function (oSettings, aData, iDataIndex) {
    if (($('#fromDate').length > 0 && $('#fromDate').val() !== '') || ($('#toDate').length > 0 && $('#toDate').val() !== '')) {
        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth();
        var yyyy = today.getFullYear();
console.log(today+"-- "+dd+" --"+mm+" --"+yyyy);
        if (dd < 10) dd = '0' + dd;

        if (mm < 10) mm = '0' + mm;

        today = mm + '/' + dd + '/' + yyyy;
        var minVal = $('#fromDate').val();
        var maxVal = $('#toDate').val();
        //alert(minVal+"   ----   "+maxVal);
        if (minVal !== '' || maxVal !== '') {
            var iMin_temp = minVal;
            if (iMin_temp === '') {
                iMin_temp = '01/01/1980';
            }

            var iMax_temp = maxVal;
            var arr_min = iMin_temp.split("/");

            var arr_date = aData[2].split("/");
//console.log(arr_min[2]+"-- "+arr_min[0]+" --"+arr_min[1]);
             var iMin = new Date(arr_min[2], arr_min[0]-1, arr_min[1]);
          //  console.log(iMin);
           // console.log(" --"+yyy);
           

            var iMax = '';
            if (iMax_temp != '') {
                var arr_max = iMax_temp.split("/");
                iMax = new Date(arr_max[2], arr_max[0]-1, arr_max[1], 0, 0, 0, 0);
            }




            var iDate = new Date(arr_date[2], arr_date[0]-1, arr_date[1], 0, 0,...