mRender and timezone....

mRender and timezone....

luisrortegaluisrortega Posts: 79Questions: 6Answers: 1

hi, I'm calculating the "time since" on a column using mRender, to do so, I added a class to a span, and within a timer I select them and recalculate them. This way I don't refresh all data, but the one that I have to change.

If the user is in the same timezone as the server, no problem, however if the user is in a different timezone, mRender cut the timezone out of the date... is there a way for me to retrieve it?

thanks,
Luis

// table declaration...
{ "sTitle": "Wait" , "width": "100px",
"mRender": function (data, type, row) {
return '<span dt="' + row.dt + '" class="wait_secs" >' + 0 + '</span>';
},

//timer
var update_signal_wait = setInterval( function(){
// wait since signal arrival
var items = $(".wait_secs"); //get all visible items
$.each( items, function( i, item ) {

                   var cur = new Date();
                   var sdt = new Date(item.getAttribute("dt"));
                   var diff = (cur.getTime() - sdt.getTime()) / 1000;
                   var res = '';

                   if ( diff <60)
                     res = diff.toFixed(1).toString() + ' s';
                     else {
                       diff = diff / 60;
                       if ( diff <60)
                         res = diff.toFixed(1).toString() + ' m';
                         else {
                           diff = diff / 60;
                           res = diff.toFixed(1).toString() + ' h';
                           }
                   }
                   item.innerHTML = res;

                  });



                },3000);

Replies

  • luisrortegaluisrortega Posts: 79Questions: 6Answers: 1

    Solved, well I did a work around... I passed the server timezone as a local javascript variable and add that server timezone to the render function...

    //server generated "constant" of it's timezone...
    var server_timezone = "GMT-04";

    then changed my mRender to append this at the end of the date field

                            { "sTitle": "Wait" , "width": "100px",
                              "mRender": function (data, type, row) {
                                  console.log('prev type ',data);
                                  return '<span dt="' + row.dt + ' ' + server_timezone + '" class="wait_secs" >' + 0 + '</span>';
                                 },
                              "asSorting": [ "desc" ,"asc"]
                            },
    

    I'll have to test it on positive and negative UTC, to be sure... but so far on my case is working (I'm on -4 and my server is on -7) and it calculate appropiately...

    I would love if I can read the true value out of DataTables, so I'm not appending string together hoping it won't fail... O.o

    Any suggestion is welcome!

    Tks,
    Luis

  • luisrortegaluisrortega Posts: 79Questions: 6Answers: 1

    I just noticed DataTables shows server time... I wonder if there is any easy way to show translated client time?

  • luisrortegaluisrortega Posts: 79Questions: 6Answers: 1

    And I keep stumble right here! pardon for wasting your time here... DataTables show the actual UTC time (zero).... at that point all I needed was to add GMT to the mRender... this makes the function works perfect regardless of the server or client timezone...

    Now my question is... how to display current (client end) timezone? I assume that I can use mRender to do the translation, but wonder if there is a build in already in place.

    Luis

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    Hi Luis,

    There isn't any built in date / time handling in DataTables, other than what you can utilise in regular Javascript. However, you could use MomentJS to convert from UTF to local. See this section of their docs.

    Allan

  • luisrortegaluisrortega Posts: 79Questions: 6Answers: 1

    I'll take a look, Tks Allan

This discussion has been closed.