i nedd a filter to EU Date

i nedd a filter to EU Date

Rectec013Rectec013 Posts: 42Questions: 10Answers: 0

hello guys ,
i need a filter for my datatable for EU Date (like "22.11.2009").
wich plugin can do this?
thanks

Answers

  • rf1234rf1234 Posts: 2,941Questions: 87Answers: 415

    Moment.js

  • allanallan Posts: 63,192Questions: 1Answers: 10,412 Site admin

    The thirdparty YADCF probably does this. Alternatively, you could write your own.

    Allan

  • Rectec013Rectec013 Posts: 42Questions: 10Answers: 0

    thanks , it doesn t work on server side processing

  • allanallan Posts: 63,192Questions: 1Answers: 10,412 Site admin

    No indeed it doesn't. You'd need to write your own if you are using server-side processing I'm afraid.

    Allan

  • rf1234rf1234 Posts: 2,941Questions: 87Answers: 415

    There are two ways to do this:
    1. Manipulate search value on the client side to match database format
    2. Use a view on the server side that returns the right client side date format so that it can be searched for on the server side (a get formatter won't work because the search is applied to the values coming from the database BEFORE get formatting).

    1. is easy - just make the view
    2. here is an example that converts long and short German (31.12.2019) and English UK dates (31/12/2019; not American!) to the database format and does the same with numbers (get rid of thousand separators and decimal commas)
    ....
    data: function ( d ) {
    //allow searching for dates with server side processing
        var dFs = ['D/M/YYYY', 'DD/MM/YYYY', 'D/M/YY', 'DD/MM/YY', 'D/M', 'DD/MM', 'D/M/', 'DD/MM/'];
        var toBeFormat = ''; var sepCounter = 0;
    //No commas allowed as date separator; if English: no period either!
        if ( ( ! ( d.search.value.indexOf(',') >= 0 ) ) &&
             ( ! ( lang !== 'de' && d.search.value.indexOf('.') >= 0 ) )    )   {
            if ( moment(d.search.value, dFs).isValid() ) {
                toBeFormat = 'MM-DD';
                for (i=0; i < d.search.value.length; i++) {
                    //counting the separators in the search string
                    if ( isNaN (d.search.value.substr(i, 1)) ) {
                        sepCounter++;
                //if we find two separators and the second one is not at the
                //end of the string we have a long date otherwise only a short one
                        if ( sepCounter === 2 && i < (d.search.value.length-1) ) {
                            toBeFormat = 'YYYY-MM-DD';
                        }
                    }                        
                }
                if (sepCounter > 0) { //we need to have found at least one separator
                    d.search.value = moment(d.search.value, dFs).format(toBeFormat);
                } else {
                    toBeFormat = '';
                }
            }
        }
    //not a date: we check for a number
        if (toBeFormat <= '') {
            var number;
            if (lang == 'de') {
                number = d.search.value.toString().replace( /[\.]/g, "" );
                number = d.search.value.toString().replace( /[\,]/g, "." );
            } else {
                number = d.search.value.toString().replace( /[\,]/g, "" );
            }
            if ( ! isNaN( parseFloat(number) ) ) {             
                d.search.value = number;
            }
        }
    }
    
This discussion has been closed.