简体   繁体   English

将Bootstrap日期范围Datepicker更新为仅允许结束日期在开始日期的会计年度内

[英]Update Bootstrap Date Range Datepicker to only allow for end date to be within the start date's fiscal year

I'd like to preface this saying I know very little JavaScript. 我想在这句话前言,我对JavaScript的了解很少。

I have a Bootstrap datepicker using a date range, picked up from eternicode . 我有一个使用日期范围的Bootstrap datepicker ,该日期范围是从eternicode拾取的。

My goal is to select a day in startDate and then have the days available to endDate only be on or after startDate and within startDate's financial year. 我的目标是在startDate中选择一天,然后将可用于endDate的日期设置为仅在startDate或之后,并且在startDate的财政年度内。

Examples: 例子:

  • If I chose Oct 1st 2016, the endDate should cap out at Sep 30th 2017 如果我选择2016年10月1日,那么endDate应该在2017年9月30日结束

  • If I chose Jan 12th 2017, the endDate should cap out at Sep 30th 2017 如果我选择2017年1月12日,那么endDate应该在2017年9月30日结束

  • If I chose Sep 30th 2017, the endDate should also be Sep 30th 2017 如果我选择2017年9月30日,那么endDate也应该是2017年9月30日

  • Pretty much, if the startDate month is in [Oct, Nov, Dec] then endDate caps out at Sep 30th (startDate year + 1) else endDate caps out at Sep 30th (startDate year) 差不多,如果startDate月份为[Oct,Nov,Dec],那么endDate的上限为9月30日(startDate年+ 1),否则endDate的上限为9月30日(startDate年)

In my reportWebpage.cshtml file I have: 在我的reportWebpage.cshtml文件中,我有:

<div class="col-md-2">
    Date Range:
</div>
<div class="col-md-5" id="dateRangeContainer">
    <div class="input-daterange input-group" id="datepicker">
        @Html.TextBox("startDate", "", new { @class = "input-sm form-control", name= "startDate" })
        <span class="input-group-addon">to</span>
        @Html.TextBox("endDate", "", new { @class = "input-sm form-control", name = "endDate" })
    </div>
</div>

In my related.js I have a very basic datepicker setup: 在我的related.js中,我有一个非常基本的datepicker设置:

$(function () {
    $('#dateRangeContainer .input-daterange').datepicker({
        autoclose: true,
        todayBtn: "linked",
        clearBtn: true
    });
});

I know there is a datesDisable property I can set, and while it's functionality is what I want it seems to be based off an array of dates which seems like the wrong idea here. 我知道我可以设置一个datesDisable属性,虽然它是我想要的功能,但它似乎基于日期数组,这似乎是错误的主意。


As a test, I replaced the datapicker js code above with what is shown below. 作为测试,我用下面显示的内容替换了上面的datapicker js代码。

Like in this SO Answer I've tried adding an onSelect property to just the #startDate datepicker, but I'm not getting a response from the alert embedded, nor is Google Chrome's Dev Tools hitting the debug point placed on it: 像在此《 答案》中一样,我尝试将onSelect属性仅添加到#startDate日期选择器中,但是我没有从嵌入的警报中得到响应,谷歌浏览器的开发工具也没有碰到放置在其上的调试点:

$('#startDate').datepicker({
    onSelect: function() {
        var date = $(this).datepicker('getDate'),
            day = date.getDate(),
            month = date.getMonth() + 1, // Offset the zero index to match normal month indexes
            year = date.getFullYear();
        alert(day + '-' + month + '-' + year);
    }
});

I was hoping that by doing that I could at least start building some if else loops or something. 我希望这样做,至少可以开始构建一些其他循环或其他东西。

I'm struggling to figure out how to even start with this problem, so any help or suggestions will be very much appreciated! 我正在努力弄清楚如何开始解决这个问题,因此,我们将不胜感激任何帮助或建议!



Edit 1: I figured out that trying to disable a huge range of dates was the wrong way to view this problem, and that I should instead focus on utilizing the setStartDate and setEndDate properties. 编辑1:我发现尝试禁用大量日期是查看此问题的错误方法,而我应该集中精力利用setStartDate和setEndDate属性。

Using some combined tips from these SO answers: 使用这些SO答案中的一些组合技巧:

I mashed together this JSFiddle: http://jsfiddle.net/wsodjsyv/203/ 我将这个JSFiddle混在一起: http : //jsfiddle.net/wsodjsyv/203/

Where it currently is, it does it's job of restricting to the proper financial year. 当前位置是在限制适当的财政年度。 I just need to tweak it so that when I clear End Date I'm able to move Start Date past that point again. 我只需要对其进行调整,以便在清除“结束日期”时能够再次将“开始日期”移到该点之后。 Right now it'll require a refresh if I determine I want to move Start Date past Sept. 30 (whatever year got chosen) 现在,如果我确定要将“开始日期”移到9月30日之后(无论选择哪个年份),都需要刷新

Here is my new related.js file pertaining to the date picker; 这是我与日期选择器有关的新的related.js文件; with this code I am able to restrict the date range to Start Date's fiscal year: 使用此代码,我可以将日期范围限制为开始日期的会计年度:

$(function () {
    // Ranged datepickers
    $('#dateRangeContainer .input-daterange').datepicker({
        autoclose: true,
        todayBtn: "linked",
        clearBtn: true
    });

    $('#startDate').datepicker().on('changeDate', function(selected) {
        var endDate_Bottom = null;
        var endDate_Cap = null;

        if (selected.date != null) {
            endDate_Bottom = new Date(selected.date.valueOf());
            endDate_Cap = new Date(selected.date.valueOf());

            if (endDate_Bottom.getMonth() >= 9 && endDate_Bottom.getMonth() <= 11) {
                endDate_Cap = new Date(endDate_Bottom.getFullYear() + 1, 8, 30);
            } else {
                endDate_Cap = new Date(endDate_Bottom.getFullYear(), 8, 30);
            }
        }

        $('#endDate').datepicker('setStartDate', endDate_Bottom);
        $('#endDate').datepicker('setEndDate', endDate_Cap);
    });

    $("#endDate").datepicker().on('changeDate', function(selected) {
        var startDate_Cap = null;
        if (selected.date != null) {
            startDate_Cap = new Date(selected.date.valueOf());
        }
        $('#startDate').datepicker('setEndDate', startDate_Cap);
    }).on('clearDate', function(selected) {
        var startDate_Cap = null;
        $('#startDate').datepicker('setEndDate', startDate_Cap);
    });
});

I've added some checks for when the date is null to avoid the console log being filled with errors when .valueOf() is called on an empty date. 我添加了一些检查以确保日期为null,以避免在空日期调用.valueOf()时控制台日志被错误填充。

I also brought back the dateRangeContainer block to handle the repeated options and to allow for the styling that highlights the date range selected in the calendar. 我还带回了dateRangeContainer块,以处理重复的选项,并允许突出显示日历中选择的日期范围的样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 引导日期选择器日期范围结束日期更新 - Bootstrap datepicker date range end date updating 引导日期选择器:从年份开始选择日期 - Bootstrap datepicker: Start picking date from year 在“日期范围”中仅选择开始日期,并将“结束”设置为+7天(引导程序日期范围选择器) - Select only start date in Date Range and Set +7 days for End (Bootstrap Date Range Picker) 开始日期和结束日期日期选择器 - Start date and end date datepicker Twitter Bootstrap Datepicker-开始日期小于结束日期 - Twitter bootstrap datepicker - start date less than end date 我希望在jQuery r Bootstrap中将END日期中的datepicker作为当前日期并将START日期作为END日期之前7天 - I want datepicker in that END date as current date and START date as 7 days previous to END date in Jquery r Bootstrap (引导程序)Vue Datepicker:如何定义开始和结束日期,使结束日期不能早于开始日期? - (Bootstrap) Vue Datepicker: How to define start and end date, so that the end date can't be before the start date? 我如何只允许从日期选择器中选择从今天起最多一年的时间? - How can I only allow up to one year from today's date to be selected from datepicker? 减少引导日期选择器的结束日期 - Decrease bootstrap datepicker end date Bootstrap Datepicker未清除日期范围 - Bootstrap Datepicker not clearing for date range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM