Time comparison in Datatables

Time comparison in Datatables

mihalispmihalisp Posts: 127Questions: 22Answers: 0

Hello,
I am trying to compare date and time in a renders column where i want to compute 2 dates difference in "HH:mm".

I use servber side script where i get the dates format (and pass them in client Datatable) as follows:

->getFormatter( Format::dateSqlToFormat( 'd/m/Y H:i' ) ). //data.start and data.end columns in Datatable

i have used many different code but with no success.
can you suggest a safe method?
I want it in client side,but i would also like a sample in server side script (for example a function render in serverside?)

eg.

         ....."render":  function (data, type, row) { 
          var date1, date2;
          date1 = data.start ;
          //date1 = moment(data.start).locale('el').format('DD/MM/YYYY HH:mm'); invalid date

          console.log(typeof date1);
          console.log('date1 =' + date1); //date1 =18/06/2019 21:38 -  string

          //date2 = new Date( data.end );  invalid date
          date2 = data.end;  
          console.log(typeof date2);
          console.log('date2 =' + date2); //18/06/2019 12:00 - string

or

          var time1 = moment(date1, "HH:mm");
          console.log(typeof time1);
          console.log('time1 =' + time1); // 1560958680000  - object

          var time2 = moment(date2, "HH:mm");
          console.log(typeof time2);
          console.log('time2 =' + time2);// 1560958640000  - object

          var d = moment.duration(time1.diff(time2));
          var s = moment(+d).format('HH:mm');  // format a string result     
          console.log(typeof s);
          console.log("s = " + s); // string  s = 02:38 wrong difference

or

      var res = Math.abs(wrario1 - wrario2) / 1000 ;  
      console.log(typeof res);
      console.log('res' +res);
      var hours, minutes ; 

     hours = Math.floor(res / 3600) % 24;  
     if (hours < 10) { hours = "0"+hours ; }

     minutes = Math.floor(res / 60) % 60;
     if (minutes < 10) {  minutes = "0"+minutes ; }
       else  { minutes = minutes ;} 

     var calculation = hours+ ":" +minutes  ;

Can anyone help please?

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Hi @mihalisp ,

    I'd say use Moment.js, as it's excellent for all time/date based operations. If it's not working, can you post a test case and we'll take a look,

    Cheers,

    Colin

  • mihalispmihalisp Posts: 127Questions: 22Answers: 0

    I have tried with moment.js but with no success.

        date1 = moment(data.start).locale('el').format('DD/MM/YYYY HH:mm'); // returns invalid date
    

    Also,
    In my 2nd case it doesn't return the right difference.
    Does the code seem ok?

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Hi @mihalisp ,

    Could you knock out a test case that shows this, please. If you can show your date formats then that will give us something to work with,

    Cheers,

    Colin

  • mihalispmihalisp Posts: 127Questions: 22Answers: 0

    Here is the test case,i just want the time difference.

    http://live.datatables.net/jequcipa/1/edit

    Thanks a lot!

  • mihalispmihalisp Posts: 127Questions: 22Answers: 0

    Forgive me for this irrelevant question,but when do you plan (if you plan) to support colspan or rowspan in the tbody ? It is a necessary and really great improvement in Datatables at the moment.

    Thank you

  • kthorngrenkthorngren Posts: 20,275Questions: 26Answers: 4,765

    Here is the test case,i just want the time difference.

    Thanks. As Colin mentioned it would work best to use moment.js. A search yields this SO thread. Lots of options to try there.

    Tried a couple from the thread and this seems to work best:
    http://live.datatables.net/dedoqiju/1/edit

    For the rowspan / colspan question I'll leave it to @allan to answer.

    Kevin

  • mihalispmihalisp Posts: 127Questions: 22Answers: 0

    Well,row[3] didn't work for me,it says Invalid date,
    but data.startdate worked.

    Thank you guys!

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin

    to support colspan or rowspan in the tbody ?

    Not at this time. It would be a large undertaking (like a complete rewrite) to do it properly. It would also probably require the DOM elements to be destroyed and recreated on redraw, which I'm not keen on (although funnily that is what DataTables did until v1.5).

    but data.startdate worked.

    Sounds like you are using objects as your data source rather than arrays for rows, so yes, getting the object property you want would be correct.

    Allan

This discussion has been closed.