本文介绍了日期天差应该不超过20天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户输入了起始日期和日期,则我需要的起始日期和起始日期之间的时间间隔不应为20天.即如果用户输入的日期从date = '30/08/2018'到date = '26/09/2018',则间隔超过20天,所以我想使用jquery显示警报.下面是我的代码

I am having fromdate and todate I want if user enters the from date and to date the gap between them should not be ore then 20 days. i.e if user enters from date='30/08/2018' to date='26/09/2018' here the gap is more then 20 days so i want to show a alert using jquery.Below is my code

 var today = new Date(new Date().getFullYear(), new Date().getMonth(),new Date().getDate());
 $('#startdate').datepicker({
    uiLibrary : 'bootstrap4',
    iconsLibrary : 'fontawesome',
    format : 'dd/mm/yyyy',
    maxDate : function() {
        return $('#enddate').val();
    }
 });
 $('#enddate').datepicker({
    uiLibrary : 'bootstrap4',
    iconsLibrary : 'fontawesome',
    format : 'dd/mm/yyyy',
    minDate : function() {
        return $('#startdate').val();
    }
 });

推荐答案

处理两个输入中的onchanged事件,并创建一个函数'checkDates()',该函数将比较两个日期,如果相差超过20天,则发出警报().请参见下面的示例代码

handle onchanged event in both the inputs and make a function 'checkDates()' which will compare the two dates and if the difference is more then 20 days make alert() .See the example code below

<input id="startdate" onchanged="checkDate()"/>
<input id="enddate" onchanged="checkDate()"/>
<script>
    function checkDate(){
        var start = $('#startdate').val();
        var end = $('#enddate').val();
        //convert strings to date for comparing
        var startDate = new Date(start);
        var endDate = new Date(end);
        // Calculate the day diffrence
        var oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
        var diffDays = Math.abs((endDate.getTime() - startDate.getTime()) / (oneDay));
        if(diffDays > 20){
             alert("Days are more then twenty");
        }
    }
</script>

请让我知道它是否有效.

Please let me know if it worked.

这篇关于日期天差应该不超过20天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:46