簡體   English   中英

Twitter Bootstrap datepicker:如何僅啟用特定的日期范圍

[英]Twitter Bootstrap datepicker: how to enable only specific daterange

我正在開發一些基於Twitter Bootstrap的項目,該項目使用來自https://github.com/eternicode/bootstrap-datepicker的日期選擇器(這是其他版本的一個分支),但它缺少一個我需要的非常重要的功能 -如何僅啟用特定日期范圍(例如,從過去15天到今天),因此無法選擇任何其他日期(不可點擊)。

我在SO上找到了一個類似的解決方案,它禁用星期六和星期日: 限制bootstrap-datepicker到工作日? http://jsfiddle.net/katowulf/zNbUT/5/ ,但我不知道如何根據我的需要調整它。

提前致謝。

您鏈接的類似問題最終使用了很多黑客,但您想要的更簡單 - 您正在尋找startDateendDate選項:

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

這些選項可以使用Date對象,timedelta字符串(就像我在這里所做的那樣),或者遵循給定format日期字符串。

演示: http//jsfiddle.net/zNbUT/131/

此外,請確保您使用的是github中的代碼 - eyecon.ro的副本是原始代碼,包含舊錯誤且沒有新功能。

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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM