How to order by date when it is rendered custom

How to order by date when it is rendered custom

WebCodexWebCodex Posts: 71Questions: 13Answers: 3

I am currently rendering my date as a custom value but want the table to be ordered by it when it is generated, the date from the database looks like this: 2018-06-04 08:33:09

Here is my code:

                               ``` { 'data': 'recipe_added', 
                    "render": function ( data, type, row, meta ) {
                        // Create date from input value
                        var inputDate = new Date(data);

                        // Get today's date
                        var todaysDate = new Date();



                        // call setHours to take the time out of the comparison
                        if(inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)) {
                            var d = new Date(data);
                            var hour = d.getHours();
                            var minute = d.getMinutes();
                            return 'Today ' + hour +':'+ minute;
                        } else if(d == yesterday) {
                            var date = new Date();
                            // Get yesterday's date
                            var yesterday = date.setDate(todaysDate.getDate() - 1);
                            var d = new Date(data);
                            var hour = d.getHours();
                            var minute = d.getMinutes();
                            return 'Yesterday ' + hour +':'+ minute;
                        }else {
                            var d = new Date(data);
                            var month = d.getMonth()+1;
                            var day = d.getDate();
                            var hour = d.getHours();
                            var minute = d.getMinutes();
                            recipeadded = (day<10 ? '0' : '') + day + '-' + (month<10 ? '0' : '') + month + '-' + d.getFullYear() + ' ' + hour +':'+ minute;
                            return recipeadded;
                        }
                    },  
                },```

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Hi @WebCodex ,

    moment.js is a good library for doing that. This plug-in here uses it, and can be a good fix for you,

    Cheers,

    Colin

  • WebCodexWebCodex Posts: 71Questions: 13Answers: 3

    Interestingly I stumbled upon this while searching a solution, I don't really understand how I can implement it in my current code.

  • WebCodexWebCodex Posts: 71Questions: 13Answers: 3

    As you can see I render the date as Today if it is today and Yesterday if it was yesterday, is there a way to tell DataTables to sort by the original data?

  • WebCodexWebCodex Posts: 71Questions: 13Answers: 3

    Thanks Colin, that worked :)

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @WebCodex ,

    It looks like you're having a conversation with yourself there! :smile: From your last line, I'm guessing all is good now?

    Cheers,

    Colin

This discussion has been closed.