简体   繁体   English

Moment.js 仅在第 12 天之前过滤

[英]Moment.js filtering only before day 12

I have a datatable and using moment for filtering date.我有一个数据表并使用时刻来过滤日期。 My problem is if i enter days before day 12 is working fine but after day 12 not filtering my data.我的问题是,如果我在第 12 天之前输入几天工作正常,但在第 12 天之后没有过滤我的数据。 For example the date between 01/01/2021 and 12/01/2021 is working fine but if i enter 01/01/2021 and 13/01/2021 not getting any data.例如,01/01/2021 和 12/01/2021 之间的日期工作正常,但如果我输入 01/01/2021 和 13/01/2021 没有得到任何数据。

// Extend dataTables search
        $.fn.dataTable.ext.search.push(
            function (settings, data, dataIndex) {
                var min = $('#min-date').val();
                var max = $('#max-date').val();
                var createdAt = data[0] || 0; // Our date column in the table
                moment().format('DD/MM/YYYY');

                if (
                    (min == "" || max == "")
                    ||
                    (moment(createdAt, 'DD/MM/YYYY').isSameOrAfter(min, 'DD/MM/YYYY') && moment(createdAt, 'DD/MM/YYYY').isSameOrBefore(max, 'DD/MM/YYYY'))
                ) {
                    return true;
                }
                return false;
            }
        );

 function dtConvFromJSON(data) {
            if (data == null) return '1/1/1950';
            var r = /\/Date\(([0-9]+)\)\//gi
            var matches = data.match(r);
            if (matches == null) return '1/1/1950';
            var result = matches.toString().substring(6, 19);
            var epochMilliseconds = result.replace(
                /^\/Date\(([0-9]+)([+-][0-9]{4})?\)\/$/,
                '$1');
            var b = new Date(parseInt(epochMilliseconds));
            var c = new Date(b.toString());
            var curr_date = c.getDate();
            var curr_month = c.getMonth() + 1;
            var curr_year = c.getFullYear();
            var curr_h = c.getHours();
            var curr_m = c.getMinutes();
            var curr_s = c.getSeconds();
            var curr_offset = c.getTimezoneOffset() / 60
            var d = curr_date.toString() + '/' + curr_month.toString() + '/' + curr_year.toString();
            return d;
        }

Iam using dtConvFromJson function here;我在这里使用 dtConvFromJson function;

  "columns": [
                { "data": "Date", render: function (data, type, full) { return dtConvFromJSON(data); }, "autoWidth": true },

I fixed to problem.我解决了问题。

I have changed my function dtConvFromJSON(data) function to我已将 function dtConvFromJSON(data) function 更改为

 function dtConvFromJSON(data) {
  return new Date(parseInt(data.replace('/Date(', '')))
}

and voila!瞧! Works now!现在工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM