Date column having value like Aug 1,2017 sorting as string instead date

Date column having value like Aug 1,2017 sorting as string instead date

ovessovess Posts: 15Questions: 1Answers: 0

I am populating data table using django .One of the column named Date consists values in the formatlike Aug 1,2017.
There is sorting issue as that date column sorting like string.
August 1,2017
July 10,2017
July 11,2017

Answers

  • rf1234rf1234 Posts: 2,809Questions: 85Answers: 406

    Just use moment.js!

    I only had to add one line of code to fix this:

    var momentLocale;
    
    if (lang === 'de') {
        moment.locale('de');
        momentLocale = 'de';
    } else {
        moment.locale('en-gb');
        momentLocale = 'en-gb';
    }
    
    //set parms for date sorting using moment.js
    $.fn.dataTable.moment( 'L', momentLocale );
    

    https://momentjs.com/

    You will probably need this:

    moment.locale('en');
    
    //set parms for date sorting using moment.js
    $.fn.dataTable.moment('LL', 'en' );
    

    This is the datatables plugin: https://datatables.net/plug-ins/sorting/datetime-moment

    <script src="https://cdn.datatables.net/plug-ins/1.10.15/sorting/datetime-moment.js"></script>
    

    and of course you need to install moment.js. For the US I don't think you need to install additional locales. In case you do need to install the US locale a link like this should work:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/locale/en.js"></script>
    
  • ovessovess Posts: 15Questions: 1Answers: 0

    your suggestion not worked. problems still persists

  • rf1234rf1234 Posts: 2,809Questions: 85Answers: 406

    If you really have this format “August 1,2017“ with no blank between the comma and the year this might not be recognized. Just check the moment.js docs. I can reassure you moment and the plugin work ok.

  • ovessovess Posts: 15Questions: 1Answers: 0

    sorting working fine in Chrome but not in Mozella Firefox

  • rf1234rf1234 Posts: 2,809Questions: 85Answers: 406
    edited August 2017

    just tried mine in Firefox and it worked. Sorry, I am not a browser specialist ... and without seeing a single line of your code I am pretty sure that nobody is going to be able to help you out with this ...

    @allan might be able to help you but you would need to provide your code and also run the data tables debugger on the table please: https://debug.datatables.net/

  • rf1234rf1234 Posts: 2,809Questions: 85Answers: 406

    Well I said: "For the US I don't think you need to install additional locales. In case you do need to install the US locale a link like this should work:"

    So this recommendation was just in case a locale file for the US was required. But it probably isn't! So why don't you try it without the locale file! (I can reassure you for languages other than US English you will need a locale file!).

    Here you see all the available locale files. And yes, there is none for US English because it is the default language.
    https://cdnjs.com/libraries/moment.js/

  • ovessovess Posts: 15Questions: 1Answers: 0

    as evident in attached screenshot, date is starting from July 9,2017 instead of July 31,2017.
    so this is the sorting issue i am having in Mozella Firefox

  • rf1234rf1234 Posts: 2,809Questions: 85Answers: 406

    maybe this is caused by the 404 error? I would get rid of the error and try again ...

  • ovessovess Posts: 15Questions: 1Answers: 0

    in another attached screenshot, it is shown that after August 7,2017, next date should be July 31,2017 but it is displaying July 10,2017.
    kindly help.

  • allanallan Posts: 61,864Questions: 1Answers: 10,136 Site admin

    Yes please, could you use the debugger as @rf1234 mentioned so we can see the setup.

    Even better would be a link to the page showing the issue.

    Allan

  • ovessovess Posts: 15Questions: 1Answers: 0
  • ovessovess Posts: 15Questions: 1Answers: 0

    kindly look into the attached html . sorting not working in Mozella

  • allanallan Posts: 61,864Questions: 1Answers: 10,136 Site admin

    Your date strings are:

    July 9, 2017

    But the Moment documentation says that LL formatted strings should be like: September 4 1986 - i.e. no comma.

    So your date format doesn't match what it is being told to look for.

    Allan

  • ovessovess Posts: 15Questions: 1Answers: 0

    than what to do in my case to fix sorting problem in mozella

  • rf1234rf1234 Posts: 2,809Questions: 85Answers: 406

    Found this in the moment.js source code:

    var defaultLongDateFormat = {
        LTS  : 'h:mm:ss A',
        LT   : 'h:mm A',
        L    : 'MM/DD/YYYY',
        LL   : 'MMMM D, YYYY',
        LLL  : 'MMMM D, YYYY h:mm A',
        LLLL : 'dddd, MMMM D, YYYY h:mm A'
    };
    

    Since US English is the default this is what moment uses if you don't specify a locale.

    Just try this (without specifying a locale). It should still work in Chrome. If it doesn't work in Firefox then it is a browser or a moment.js problem which I don't think we can resolve here for you ...

    $.fn.dataTable.moment('MMMM D, YYYY' );
    
  • ovessovess Posts: 15Questions: 1Answers: 0

    i tried it
    $.fn.dataTable.moment('MMMM D, YYYY' );
    but nothing changes in mozella. :(

  • rf1234rf1234 Posts: 2,809Questions: 85Answers: 406

    Since it works in Chrome you should get in touch with the Firefox support. I don't think it is a moment.js problem because for me it also works in Firefox. There must be something special in your application that causes the problem with Firefox ... Good luck with the Firefox support ...

  • allanallan Posts: 61,864Questions: 1Answers: 10,136 Site admin

    Can you give me another debug trace with the latest version of your code please.

    Allan

  • ovessovess Posts: 15Questions: 1Answers: 0
  • rf1234rf1234 Posts: 2,809Questions: 85Answers: 406

    @ovess: 404 - Page not found ... try again pls

  • ovessovess Posts: 15Questions: 1Answers: 0

    On Which page?

  • tangerinetangerine Posts: 3,350Questions: 37Answers: 394
  • ovessovess Posts: 15Questions: 1Answers: 0
  • rf1234rf1234 Posts: 2,809Questions: 85Answers: 406

    No tables! I guess you need to do this once you have the page with your table(s) opened in Firefox. Otherwise there are no table data. Please read the instructions carefully. I recommend to use the bookmarklet.

  • ovessovess Posts: 15Questions: 1Answers: 0
  • allanallan Posts: 61,864Questions: 1Answers: 10,136 Site admin

    If the debugger shows 0 tables (like in the ajoloc trace) and you think you have created a DataTable, it normally means that there has been a Javascript error during the initialisation of the table.

    Have a look at your browser's console.

    Allan

This discussion has been closed.