Daterangepicker:选择日期时如何阻止开始日期和结束日期相同?

发布于 2025-01-14 06:34:11 字数 862 浏览 3 评论 0原文

我在我的网站上使用 JQuery 的 daterangepicker 插件,使用户能够选择预订房间的入住和退房日期。我已确保用户无法选择任何已经过去的日期,但我的问题是,当用户选择特定日期作为其入住日期时,他们将能够再次选择相同的日期作为其退房日期,这就是我想避免他们这样做。例如,如果他们选择 2022 年 3 月 14 日作为入住日期,则可以再次选择 2022 年 3 月 14 日作为退房日期。

这是我为实现这项工作所做的努力:

$(".checkin").daterangepicker({
    autoApply: true,
    alwaysShowCalendars: true,
    formatDate: 'MM/DD/YYYY', // Format i chose for setting how date should look
    startDate: moment(), // I set the starting date to be the current date
    endDate: moment().add(1, 'days'), // I set the no of days not to exceed 1 day but it made only the current day selectable
    minDate: moment(),
    maxDate: moment().add(1, "days"),
    clickDate(): function(e) { // I saw this method in the daterangepicker object but it didn't work
        console.log("Specified date clicked");
    }
});

谁可以帮助我解决这个问题?

I am using JQuery's daterangepicker plugin on my website to enable users to select check-in and check-out dates for booking a room. I have made sure that the user cannot select any date that has already passed but my issue is when the user selects a particular date as their check in date, they would be able to select that same date again as their checkout date and this is what I want to avoid them doing. For example if they select 14th March 2022 as their check in date, they would be able to select 14th March 2022 again as their checkout date.

Here is a what I have done to try make this work:

$(".checkin").daterangepicker({
    autoApply: true,
    alwaysShowCalendars: true,
    formatDate: 'MM/DD/YYYY', // Format i chose for setting how date should look
    startDate: moment(), // I set the starting date to be the current date
    endDate: moment().add(1, 'days'), // I set the no of days not to exceed 1 day but it made only the current day selectable
    minDate: moment(),
    maxDate: moment().add(1, "days"),
    clickDate(): function(e) { // I saw this method in the daterangepicker object but it didn't work
        console.log("Specified date clicked");
    }
});

Who can help me out with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七月上 2025-01-21 06:34:11

如果您不希望用户选择确切的开始结束日期,那么您可以在daterangerpicker函数中尝试:

avoid

clickDate(): function(e) 

相反,使用下面的代码覆盖相同的日期并通过添加一天使它们连续

$('input').daterangepicker({
opens: 'left'
}, 
function(start_date,end_date) {

/*Comparing the start date and end date. if user

selected the same date then we will add one day in end date */

       if(start_date.format('DD/MM/YYYY') === end_date.format('DD/MM/YYYY')){

/* Using moment js for adding one day and overriding it into the end date

you can  call another function below to use dates */

            end_date= (moment( end_date.format('DD/MM/YYYY'), "DD/MM/YYYY").add(1, 'days'));

            $('input').data('daterangepicker').setEndDate(end_date.format('DD/MM/YYYY'));
        }
});

If you don't want the user to select the exact start and end date then you can try this in daterangerpicker function:

avoid this

clickDate(): function(e) 

Instead, Use the below code to override the same date and make them consecutive by adding one day

$('input').daterangepicker({
opens: 'left'
}, 
function(start_date,end_date) {

/*Comparing the start date and end date. if user

selected the same date then we will add one day in end date */

       if(start_date.format('DD/MM/YYYY') === end_date.format('DD/MM/YYYY')){

/* Using moment js for adding one day and overriding it into the end date

you can  call another function below to use dates */

            end_date= (moment( end_date.format('DD/MM/YYYY'), "DD/MM/YYYY").add(1, 'days'));

            $('input').data('daterangepicker').setEndDate(end_date.format('DD/MM/YYYY'));
        }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文