如何解决daterangepicker中的自定义日期范围问题?

时间:2019-02-04 14:43:23

标签: javascript php daterangepicker

if($selected_date_interval == 'Yesterday'){
 $_SESSION['start_date_by_interval'] = "moment().subtract(1, 'days')";
 $_SESSION['end_date_by_interval'] = "moment().subtract(1, 'days')";
}
elseif ($selected_date_interval == 'Today'){
 $_SESSION['start_date_by_interval'] = "moment()";
 $_SESSION['end_date_by_interval'] = "moment()";
 }
elseif($selected_date_interval == 'Last 7 Days'){
 $_SESSION['start_date_by_interval'] = "moment().subtract(6, 'days')";
 $_SESSION['end_date_by_interval'] = "moment()";
 }
elseif ($selected_date_interval == 'Last 30 Days')
{
 $_SESSION['start_date_by_interval'] = "moment().subtract(30, 'days')";
 $_SESSION['end_date_by_interval'] = "moment()";
 }
elseif ($selected_date_interval == 'This Month'){
 $_SESSION['start_date_by_interval'] = "moment().startOf('month')";
 $_SESSION['end_date_by_interval'] = "moment().endOf('month')";
 }
elseif ($selected_date_interval == 'Last Month'){
$_SESSION['start_date_by_interval'] = "moment().subtract(1, 'month').startOf('month')";
 $_SESSION['end_date_by_interval'] = "moment().subtract(1, 'month').endOf('month')";
 }else{
  $_SESSION['start_date_by_interval'] = "moment($start_date,'MMMM D, YYYY')";
  $_SESSION['end_date_by_interval'] = "moment($end_date,'MMMM D, YYYY')";
  }

所有类型的日期范围都可以正常运行,而不是自定义日期。每当我选择自定义日期时,该日期就会显示不正确。因此,对于自定义日期,我将在其他条件下传递上面的日期类型和格式。我做错了,请告诉我。

注意-我正在使用http://daterangepicker.com库。

这是我的日期选择器javascript代码。

$('#reportrange').daterangepicker({
            "showDropdowns": true,
            "autoApply": false,

            ranges: {
                'Today': [moment(), moment()],
                'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                'Last 30 Days': [moment().subtract(30, 'days'), moment()],
                'This Month': [moment().startOf('month'), moment().endOf('month')],
                'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
//                'Custom': [moment(end,'YYYY-MM-DD'), moment(start,'YYYY-MM-DD')]
//                'Custom Range': [end, start]
            },
            startDate: start,
            endDate: end,
            "locale": {
                "format": "YYYY-MM-DD",
                "separator": "-",
                "applyLabel": "Apply",
                "cancelLabel": "Cancel",
                "fromLabel": "From",
                "toLabel": "To",
                "customRangeLabel": "Custom",
                "weekLabel": "W",
                "daysOfWeek": [
                    "Su",
                    "Mo",
                    "Tu",
                    "We",
                    "Th",
                    "Fr",
                    "Sa"
                ],
                "monthNames": [
                    "January",
                    "February",
                    "March",
                    "April",
                    "May",
                    "June",
                    "July",
                    "August",
                    "September",
                    "October",
                    "November",
                    "December"
                ],
                "firstDay": 1
            },
            opens: 'left'
        }, cb);

        cb(start, end);

    });

1 个答案:

答案 0 :(得分:0)

我不确定你是否让它工作了,但我也在寻找同样问题的解决方案并解决了这个问题。

解决方法如下:

在您的代码中定义: 'Custom Range': [end, start] 而不是这个,您必须以 [moment().subtract(30, 'days'), moment()] 格式提供您的开始和结束日期。因此,您必须找到天差并提供它来代替 'moment().subtract(30, 'days')'。对我来说,这个解决方案工作正常。