我正在使用带日期选择器的jquery数据表。当日期选择器/gdc/md/<project ID>/obj/<your object ID>
为start
格式时,以下代码适用于end
和dateFormat
日期过滤器,而当yy-mm-dd
时,相同的代码不过滤表格式为dateFormat
。
当dd-mm-yy
为NaN
时,日期选择器值为dateFormat
dd-mm-yy
根据环境,呈现的表格日期列格式为let country = $('.countrySetting').text();
let countryDateFormat;
if (country == 'SE') {
countryDateFormat = 'yy-mm-dd';
} else {
countryDateFormat = 'dd-mm-yy';
}
$(".dateStart").datepicker({
autoclose: true,
showOtherMonths: true,
selectOtherMonths: true,
gotoCurrent: true,
dateFormat: countryDateFormat,
"onSelect": function (date) {
minDateFilter = new Date(date).getTime();
console.log("Min Start Date:" + minDateFilter);
oTable.draw();
}
}).keyup(function () {
minDateFilter = new Date(this.value).getTime();
oTable.draw();
});
$(".dateEnd").datepicker({
autoclose: true,
showOtherMonths: true,
selectOtherMonths: true,
gotoCurrent: true,
dateFormat: countryDateFormat,
"onSelect": function (date) {
maxDateFilter = new Date(date).getTime();
console.log("Min End Date:" + maxDateFilter);
oTable.draw();
}
}).keyup(function () {
maxDateFilter = new Date(this.value).getTime();
oTable.draw();
});
// Date range filter
minDateFilter = "";
maxDateFilter = "";
$.fn.dataTableExt.afnFiltering.push(
function (oSettings, aData, iDataIndex) {
if (typeof aData._date == 'undefined') {
aData._date = new Date(aData[0]).getTime();
console.log("Table Date:" + aData._date);
}
if (minDateFilter && !isNaN(minDateFilter)) {
if (aData._date < minDateFilter) {
return false;
}
}
if (maxDateFilter && !isNaN(maxDateFilter)) {
if (aData._date >= maxDateFilter) {
return false;
}
}
return true;
}
);
或yyyy-mm-dd HH:mm:ss
,如果呈现的表格日期列格式为dd-mm-yyyy HH:mm:ss
,则我将yyyy-mm-dd HH:mm:ss
传递为yy-mm-dd
在日期选择器初始化中,反之亦然。