twitter-bootstrap Bootstrap daterangepicker 选择的开始日期 + 30 天作为 maxDate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16478573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 17:05:56  来源:igfitidea点击:

Bootstrap daterangepicker selected start date + 30 days as maxDate

javascriptjquerytwitter-bootstrapdatepicker

提问by user774715

I was wondering if someone could help me. Using the Dan Grossman's Bootstrap Date Picker I want to add 30 days onto the selected start date and define that as the maxDate so a user can't select past this date.

我想知道是否有人可以帮助我。使用 Dan Grossman 的 Bootstrap 日期选择器,我想在选定的开始日期上添加 30 天,并将其定义为 maxDate,这样用户就不能选择超过这个日期。

I'm really stuck on this and I can't find any docummentation relating to what I want to do. Here is my code;

我真的坚持这一点,我找不到任何与我想做的事情有关的文档。这是我的代码;

var min_date = new Date();
var max_date = '05/31/2013';

$('#limited').daterangepicker(
{
    minDate: min_date,
    maxDate: max_date,
},
function(start, end) {
    $('#limited')
        .data("showAddEventDialogue_dateRangeStart", start)
        .data("showAddEventDialogue_dateRangeEnd", end);
});

Any help or advice would be very much appreciated.

任何帮助或建议将不胜感激。

Thanks a million.

太感谢了。

回答by Joshua M

It seems that the plugin only accepts a specific date format for minDateand maxDate, which is mm/dd/yyyy.

看来,该插件只接受一个特定的日期格式minDatemaxDate,这是mm/dd/yyyy

Just using new Date();unfortunately doesn't work, as it returns the full date, which is incorrect in format.

new Date();不幸的是,仅使用是行不通的,因为它返回完整的日期,而格式不正确。

The code below seems to work for me, it will use always use today as min and +30 as max.

下面的代码似乎对我有用,它将始终使用今天作为最小值和 +30 作为最大值。

/* Get the code into mm/dd/yyyy format */
function getFormatDate(d){
    return d.getMonth()+1 + '/' + d.getDate() + '/' + d.getFullYear()
}

$(document).ready(function() {
    var minDate = getFormatDate(new Date()),
    mdTemp = new Date(),
    maxDate = getFormatDate(new Date(mdTemp.setDate(mdTemp.getDate() + 30)));

    $('#daterange').daterangepicker(
    {
       minDate: minDate,
       maxDate: maxDate
    }
    );
});

回答by Roydon D' Souza

As thebrieflabbhas mentioned in his comment you can solve this problem as follows:

正如thebrieflabb在他的评论中提到的,你可以按如下方式解决这个问题:

var endDate = new Date();
endDate.setDate(startDate .getDate()+30);

You can find more your solution in this thread.

您可以在此线程中找到更多解决方案。

Hope this helped.

希望这有帮助。

回答by gotosachin

You can use like this:

你可以这样使用:

var date2 = new Date();
date2.setDate(date2.getDate()-1);
console.log(date2);
$('#daterange').daterangepicker({
    showDropdowns: false,
    format:'MM/DD/YYYY',
    maxDate:date2,
    autoclose: true,
    autoApply:true,

});

Hope it will help anyone.

希望它会帮助任何人。

回答by Bharat chaudhari

You need set maxDate and minDate like below : you should store in variable then use it.

您需要设置 maxDate 和 minDate 如下:您应该存储在变量中然后使用它。

var prev_date = new Date();
prev_date.setDate(prev_date.getDate() - 1);

$('input[name="daterange"]').daterangepicker({
        opens: 'left',
        showDropdowns: true,
        autoUpdateInput: false,
        maxDate: prev_date,
        locale: {
            cancelLabel: 'Clear',
            format: 'MMM DD, YYYY'
        }
    }); 

and if u want to allow user to select prev dates only
ex. Date of birth u can use this prop.
startDate : prev_date

如果你想允许用户只选择上一个日期
。出生日期你可以使用这个道具。
startDate : prev_date