0

As you can see in the sreenshot below, I have DOB in date format. Now I need to search max/min age based on the DOB. For example, if I input range between 10 and 15, DOB within this age range should be retrieved. (So far, it works if I have to search the age as an integer when it is already in a column.) Any idea how I can achieve this.

enter image description here

What I have tried

$.fn.dataTable.ext.search.push(
    function( dateString,settings, data, dataIndex ) {
        var min = parseInt( $('#min').val(), 10 );
        var max = parseInt( $('#max').val(), 10 );
        var age = parseFloat( data[5] ) || 0; // use data for the age column
        var today = new Date();
        var birthDate = new Date(dateString);
        // var age = today.getFullYear() - birthDate.getFullYear();
        var m = today.getMonth() - birthDate.getMonth();
 
        if ( m < 0 || (m === 0 && today.getDate() < birthDate.getDate()) || ( isNaN( min ) && isNaN( max ) ) ||
             ( isNaN( min ) && age <= max ) ||
             ( min <= age   && isNaN( max ) ) ||
             ( min <= age   && age <= max ) )
        {
            return true;
        }
        return false;
    }
);
            var table = $('#example').DataTable();
     
    // Event listener to the two range filtering inputs to redraw on input
    $('#min, #max').keyup( function() {
        table.draw();
    } );
Storm23
  • 11
  • 5
  • Do you have the dates in the DOB column as JavaScript dates or just strings? – Will Walsh Apr 28 '21 at 06:15
  • I have not yet used JavaScript. What you can see in the screenshot is mySQL DATE_FORMAT() – Storm23 Apr 28 '21 at 06:21
  • So are you asking how to do it in JavaScript DataTables or in the SQL query? – Will Walsh Apr 28 '21 at 06:22
  • I am looking for a way to do it in JavaScript DataTables. – Storm23 Apr 28 '21 at 06:30
  • It sounds like the only missing step is to convert a date to an age (integer). Have you taken a look at [this](https://stackoverflow.com/questions/4060004/calculate-age-given-the-birth-date-in-the-format-yyyymmdd) or [any of these](https://www.google.com/search?q=javascript+code+to+calculate+age+from+date+of+birth+site%3Astackoverflow.com)? Do they help? If not, what have you tried and where are you stuck? – andrewJames Apr 28 '21 at 13:03
  • I have edited my question so you can see what I have tried. – Storm23 May 05 '21 at 06:38
  • Any help with that? – Storm23 May 13 '21 at 11:02

0 Answers0