在Bootstrap datepicker中的当前日期之后180天禁用 [英] Disabling 180 days after the current date in Bootstrap datepicker

查看:135
本文介绍了在Bootstrap datepicker中的当前日期之后180天禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在处理一种表单,借此正在使用引导日期选择器向用户显示日期.有2个输入字段,显示的日期主要是出发日期返回日期.

Working on a form whereby am using bootstrap datepicker to display dates to the user. There are 2 input fields to show the dates mainly departure date and return date.

当用户选择出发日期(在日期选择器上)时,我要捕获所选日期,并且在返回日期"字段中禁用所选出发日期之后的180天.

When the user selects a departure date (on the datepicker),, I want to capture the date selected and on the return date field disable 180 days after the departure date selected.

〜请协助?

出发日期和返回日期

 <!-- Departure date-->
<div class="form-line registar2 love {{ $errors->has('departure_date') ? ' has-error' : '' }}">
    <input type="text" placeholder="Departure Date*" class="form-input dateTextBox" name="departure_date" id="departureDate" value="{{ old('departure_date') }}" required>
</div>
<!--End departure-->

<!-- Return date-->
<div class="form-line registar2 move {{ $errors->has('return_date') ? ' has-error' : '' }}">
         <input type="text" placeholder="Return Date*" class="form-input dateTextBox" name="return_date" id="returnDate" value="{{ old('return_date') }}" required>
</div>
<!-- End return date--> 

日期选择器的Javascript逻辑

 //Departure date
     $( function() {
      $( "#departureDate" ).datepicker({
        changeMonth: true,
        changeYear: true,
        //yy-mm-dd
        dateFormat: 'yy-mm-dd',
        minDate: '0',
        maxDate: "+20Y",
      });
    });

    //return date
    $( function() {
      $( "#returnDate" ).datepicker({
        changeMonth: true,
        changeYear: true,
        //yy-mm-dd
        dateFormat: 'yy-mm-dd',
        minDate: '0',
        maxDate: "+180D",
      });
    });
    //End dob

推荐答案

您必须使用moment.js来计算JavaScript中的日期.请参考代码以根据所选的选择器日期设置另一个日期选择器.

You have to use moment.js to calculate date in JavaScript. Refer the code to set another date picker according to the selected picker date.

$("#ret").hide();

$("#dep").datepicker({
    dateFormat: 'dd/mm/yy',
    minDate: 0,
    onSelect: function(dateText) {

        $("#ret").show();
        $("#ret").val('');

        var maxDate = moment(dateText, "DD/MM/YYYY").add(180, 'days').format("DD/MM/YYYY");

        $("#ret").datepicker('option', 'maxDate', maxDate);
        $("#ret").datepicker('option', 'minDate', dateText);

    }
});

$("#ret").datepicker({
    dateFormat: 'dd/mm/yy'
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>


<p>Departure Date: <input type="text" id="dep" /></p>
<p>Return Date: <input type="text" id="ret" /></p>

这篇关于在Bootstrap datepicker中的当前日期之后180天禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆