Date sort(ascending-descending) in data Table according to month and day but not work for year

Date sort(ascending-descending) in data Table according to month and day but not work for year

KashyapShivaniKashyapShivani Posts: 15Questions: 3Answers: 0

here I use date-eu.js for sorting but it doesn't work for sorting according to year,sorting work only for day and month

date-eu.js:

$.extend($.fn.dataTableExt.oSort, {

            "date-eu-asc": function (a, b) {
                console.log("inisde..js");
                if (a == '-') return 1;
                else if (b == '-') return -1;
                else {
                    var aDay = a.split('/')[0]
                    var aMonth = a.split('/')[1]
                    var aYear = a.split('/')[2]
                    var a = new Date(aMonth + "/" + aDay + "/" + aYear).getTime();

                    console.log("inisde..js a1"+a);

                    var bDay = b.split('/')[0]
                    var bMonth = b.split('/')[1]
                    var bYear = b.split('/')[2]
                    var b = new Date(bMonth + "/" + bDay + "/" + bYear).getTime();

                    console.log("inisde..js b1"+b);

                    return ((a < b) ? -1 : ((a > b) ? 1 : 0));
                }
            },

                "date-eu-desc": function (a, b) {
                if (a == '-') return 1;
                else if (b == '-') return -1;
                else {
                    var aDay = a.split('/')[0]
                    var aMonth = a.split('/')[1]
                    var aYear = a.split('/')[2]
                    var a = new Date(aMonth + "/" + aDay + "/" + aYear).getTime();

                    console.log("inisde..js a2"+a);

                    var bDay = b.split('/')[0]
                    var bMonth = b.split('/')[1]
                    var bYear = b.split('/')[2]
                    var b = new Date(bMonth + "/" + bDay + "/" + bYear).getTime();

                    console.log("inisde..js b2"+b);

                    return ((a < b) ? 1 : ((a > b) ? -1 : 0));
                }
            }
        });

I want solution that its sort for day/month and also for year.In photo you can see current output, where date sorting according day/month not year

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    I've not tried date-eu.js so not sure how it works. The recommeneded solution for properly sorting dates with Datatables is documented in this blog.

    Kevin

  • KashyapShivaniKashyapShivani Posts: 15Questions: 3Answers: 0

    Yes,Kevin I tried this also but still its doesn't sort according to year as show in photo

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736
    Answer ✓

    Its hard to say without seeing an example of what you are doing. I used the solution in the blog in this example and it looks to be working. Maybe you can update the example to replicate your sorting issue.

    Kevin

  • KashyapShivaniKashyapShivani Posts: 15Questions: 3Answers: 0

    Thank You Kevin.
    Its's work fine.

  • KashyapShivaniKashyapShivani Posts: 15Questions: 3Answers: 0
    edited October 2019

    Its work fine for MM/DD/YYYY but if date format changed to DD/MM/YYYY.
    Sorting doesn't work properly.

    Here I attach image for it

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    Did you change the format, for example $.fn.dataTable.moment( 'DD/MM/YYYY' );?

    I updated the test case to support both formats and added a second column with the day and month swapped:
    http://live.datatables.net/peqicidu/2/edit

    If this doesn't help then please provide a test case so we can see what you are doing.

    Kevin

  • KashyapShivaniKashyapShivani Posts: 15Questions: 3Answers: 0

    http://live.datatables.net/peqicidu/2/edit

    this link not open and give error

  • KashyapShivaniKashyapShivani Posts: 15Questions: 3Answers: 0

    I want solution that work for any date format because according to system or city may be date format will be change.for this type of situation solution must be work.

  • KashyapShivaniKashyapShivani Posts: 15Questions: 3Answers: 0

    I tried $.fn.dataTable.moment( 'DD/MM/YYYY' ); .But its not working properly. Its sort DD and MM in ascending and YYYY in descending.

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    Did you try the link again? Its working for me.

    I tried $.fn.dataTable.moment( 'DD/MM/YYYY' ); .But its not working properly. Its sort DD and MM in ascending and YYYY in descending.

    As mentioned before we will need to see a test case replicating the issue in order to help. Please post a link to your page or a test case so we can take a look.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    I want solution that work for any date format because according to system or city may be date format will be change.for this type of situation solution must be work.

    Datatables will attempt to automatically detect the data type in each column as documented in the columns.type. The docs state that the automatic date detection is a limited subset. Others can be added through plug-ins which is what you are doing.

    If you have data in the Start Date column that doesn't match the DD/MM/YYYY format then, since not all data matches the format, Datatables will set the column type to text and sorting won't work as expected. Do you have other data in that column?

    Kevin

  • tangerinetangerine Posts: 3,342Questions: 35Answers: 394

    04/28/2019 is not DD/MM/YYYY.

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    I modified your test case here - it seems to be behaving how you want.

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    Good eye @tangerine . I haven't had enough coffee yet :smile:

    Kevin

  • KashyapShivaniKashyapShivani Posts: 15Questions: 3Answers: 0

    Thank You .
    its work.

This discussion has been closed.