Twitter Bootstrap datepicker: how to enable only specific daterange

后端 未结 2 729
执笔经年
执笔经年 2020-12-18 03:13

I am working on some project based on Twitter Bootstrap which using a datepicker from https://github.com/eternicode/bootstrap-datepicker (that is a fork from some other vers

相关标签:
2条回答
  • 2020-12-18 03:30

    The similar question you linked to ended up using quite a hack, but what you want is a lot simpler -- you're looking for the startDate and endDate options:

    $('#dp1').datepicker({
        format: 'mm-dd-yyyy',
        startDate: '-15d',
        endDate: '+0d' // there's no convenient "right now" notation yet
    });
    

    These options can take Date objects, timedelta strings (as I did here), or date strings following the given format.

    Demo: http://jsfiddle.net/zNbUT/131/

    Also, make sure you're using the code from github -- eyecon.ro's copy is the original code, with old bugs and without the newer features.

    0 讨论(0)
  • 2020-12-18 03:48
    var startDate = new Date();
    startDate.setDate(startDate.getDate()-15);
    
    var endDate = new Date();
    
    $('#dp1')
    .datepicker().on('changeDate', function(ev){
        if (ev.date.valueOf() > endDate.valueOf()){
           alert('The date selected is too far in the future.');
        } else if (ev.date.valueOf() < startDate.valueOf()){
            alert('The date selected is too far in the past');
        } else {
            alert('fine')
        }
    

    });

    http://jsfiddle.net/XSmx6/14/

    0 讨论(0)
提交回复
热议问题