Filtering Issues due to TimeZones

Filtering Issues due to TimeZones

zgoforthzgoforth Posts: 493Questions: 98Answers: 2

These damned time zones.

I have a user on the west coast, so the issues is when he creates an item the time zone he is in is accounted for on the list item. But when I pull that item to post to a DataTable, I reformat it as so in my GET request before populating it to the table:

var transformedDate = new Date(spItem[dateKey]);

When I initialize the date columns in the table, I am also reformatting how the date is display like so:

{"data": "Days.0.Date", visible: false, orderable: false,
                    render: function(data, type, row) {
                    if (type === "sort" || type === "type") {
                        return data;
                    }
                        return data.toLocaleDateString();
                    }
                },

This shows the date as MM/DD/YYYY and because that is how the table stores it, when you filter by specific columns, it searches for how it is stored, not the original data input into the table.

I have the following filter, to search for list items that are only a part of the current week.

$.fn.dataTable.ext.search.push( //creating my own filter function for the table
  function(settings, searchData, index, rowData, counter, statusClass) { 

    const today = new Date();

    today.setMinutes(today.getMinutes() - today.getTimezoneOffset());
    const first = (today.getDate() + 1) - today.getDay(); 
    const last = first + 6;

    const monday = new Date(today.setDate(first)).toLocaleDateString();   
    const friday = new Date(today.setDate(today.getDate()+ 4)).toLocaleDateString();

    console.log("Filter Monday: " + monday); // formatted same was as the table
    console.log("Filter Friday: " + friday); // formatted same was as the table
    var sMonday = searchData[2]; //created a var to search through the 3rd column (0 index) which contains all of the Monday Dates
    var sFriday = searchData[10]; //created a var to search through the 11th column (0 index) which contains all of the Friday Dates
    console.log(sMonday);
    console.log(sFriday);
    // Get Datatables API
    var api = $.fn.dataTable.Api('#myTable');
    if (monday == sMonday && friday == sFriday) {
      $('#under_txt').html("Week Of: " + moment(mondayhead).format("MMM Do YYYY")); //applying the monday date of the current week back to the #under_txt after the search is cleared
      return true;
    }
  }
);

This works completely fine for users on the East coast, but for users on the west coast, nothing is display in the DataTable? I don't get it. I have tried all relevant JS solutions and nothing worked (time zone wise) so I was wondering if maybe it was a DataTable issue?

Answers

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

    because that is how the table stores it, when you filter by specific columns, it searches for how it is stored, not the original data input into the table.

    You can use orthogonal data for the filter operation. You can add filter to your if statement in line 3. Or maybe better to something like this:

                        if (type === "display") {
                            return data.toLocaleDateString();
                        }
                            return data;
                        }
    

    Kevin

Sign In or Register to comment.