【问题标题】:Set a jquery date picker to select a particular date range设置 jquery 日期选择器以选择特定日期范围
【发布时间】:2026-02-06 05:50:01
【问题描述】:

我有 2 个字段,DateFrom 和 DateTo,以及一个 jquery 日期选择器。如果今天是 2 月 9 日,我希望 DateFrom 字段显示 2017 年 1 月 1 日,而 DateTo 字段显示 2017 年 1 月 31 日。在这种特定情况下,我怎样才能做到这一点?

$(document).ready(function () {
    $("#ddlDocument").change(function () {
        $.ajax({
            type: "POST",
            url: serviceName + "/GetMIRReportValueAdmin",
            data: "{'Value':'" + $(this).val() + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            processdata: true,
            success: function (msg) {
                var data = msg.d;
                $('#infocontent-body').hide();
                $('#divParams').html(data);
                $('.date').datepicker({ constrainInput: true, duration: 'fast', gotoCurrent: true, dateFormat: 'd-M-yy', autoSize: true, showOn: 'focus',
                        changeMonth: true, changeYear: true, buttonImageOnly: true, buttonImage: baseUrl + 'Styles/Images/datepicker.gif',
                        minDate: new Date(curryear, 1, 1), maxDate: '+15y', yearRange: '1990:2030',
                        beforeShow: function (input, inst) { $(this).blur(); }
                    });
                ProductList(); //If there is a product list
                StatusList(); //If there is a status list

                if ($("#ddlDocument").find('option').filter(':selected').attr("aosSubparameter") == "Early Withdrawal Report") {
                    GetLifeCompanyList(); //If there is a fund list
                    $("#ddllifecompany").removeAttr('disabled');
                }
                else FinancialProductList(); //If there is a financial product list
                //FundList(); //If there is a fund list
                CompanyList(); //If there is a company List example RA and nonRA
                if ($("#ddlDocument").find('option').filter(':selected').attr("aosSubparameter") == "Investment Statement" || $("#ddlDocument").find('option').filter(':selected').attr("aosSubparameter") == "Growth Report"
                  || $("#ddlDocument").find('option').filter(':selected').attr("aosSubparameter") == "Actuaries Report") {
                    ProductFundList(); //If there is a fund list
                    GetLifeCompanyList(); //If there is a fund list
                    $("#ddllifecompany").removeAttr('disabled');
                }
            },
            error: function (msg) {
                alert('Failed to load the requested data.');
            }
        });
    });
});

【问题讨论】:

    标签: javascript jquery datepicker jquery-ui-datepicker


    【解决方案1】:

    这样的事情怎么样?

      var date = new Date(), y = date.getFullYear(), m = date.getMonth();
      var firstDay = new Date(y, m-1, 1);
      var lastDay = new Date(y, m-1 + 1, 0);
      $( ".selector" ).datepicker( "setDate", firstDay );
      $( ".selector" ).datepicker( "setDate", lastDay );
    

    【讨论】: