• Resolved Yaara

    (@yaaraher)


    I have a date column in my table, and when trying to sort it – it is being sorted alphabetically and not by date (02/01/2018 before 06/11/2017).
    How can this be fixed and is it possible to change the display format for dates?

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hey Yaara,
    had the same issue, found a solution here:
    https://datatables.net/plug-ins/sorting/datetime-moment

    Basically, the default supported date format is YYYY/MM/DD. In order to support a DD/MM/YYYY format you’d have to load moment.js & datetime-moment.js . If you’re using WordPress, you can enqueue them by adding this snippet to your functions.php (just be sure to place the JS codes in the right folder):

    wp_enqueue_script( 'moment', '/js/moment.js', array ( 'jquery' ), 1.1, true);
    wp_enqueue_script( 'datetime', '/js/datetime-moment.js', array ( 'moment' ), 1.1, true);

    Then specify the format you want to support like this:

    jQuery(document).ready(function($) { 
    // needed for running jquery in a WordPress environment
    
    $(document).ready(function() {
        $.fn.dataTable.moment( 'DD/MM/YYYY' );
        } );
    });
    Plugin Author Meitar

    (@meitar)

    Thanks, @lazarboy!

    Thread Starter Yaara

    (@yaaraher)

    I tried what you suggested and it resulted in a corrupted functions.php ??
    In wp_enqueue_script I tried several $src options but none of them worked (relative, full path, full url).
    Is functions.php the right place?

    Yikes! Sorry about that.

    To avoid corrupting functions.php, try using a snippets plugin such as this one, which allows you to safely activate and deactivate pieces of code. Where did you put moment.js and datetime-moment.js? I put them in a “js” folder in my WP installation root directory, and it works fine.

    And in case it wasn’t clear, the jQuery code should be called from a JavaScript file or added to the theme using a <script> tag, and NOT added as a snippet to functions.php… ??

    Also, I stumbled upon another solution to the date format issue. Have a look at the “date-eu” type, it might be simpler.

    Thread Starter Yaara

    (@yaaraher)

    Thank you @lazarboy!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Date format and sorting’ is closed to new replies.