如何在 jQuery datepicker 中禁用今天之前的日期

发布于 2024-11-29 04:09:51 字数 194 浏览 1 评论 0原文

如何在 jQuery 日期选择器中禁用今天之前的日期而不使用 minDate: 0

我想在今天之前像往常一样启用日历导航,同时确保用户不会选择今天之前的日期。

(即说今天的日期是 2011 年 8 月 11 日,我希望禁用此之前的所有日期,但仍然允许用户转到之前的月份、年份等)

How do I disable dates before today in jQuery datepicker WITHOUT using minDate: 0?

I would like to enable navigation of the calendar as per usual before today while making sure that user do NOT pick dates before today.

(i.e. say today's date is 11Aug11 and I would like all the dates before this disabled but still enabling user to go to previous months, years, etc.)

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

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

发布评论

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

评论(6

网白 2024-12-06 04:09:51

虽然我同意这是奇怪的行为,但您可能可以使用 onSelect< 来伪造它/code>日期选择器的事件。

$(document).ready(function() {
    $('#Date').datepicker({
        onSelect: function(dateText, inst) {
            //Get today's date at midnight
            var today = new Date();
            today = Date.parse(today.getMonth()+1+'/'+today.getDate()+'/'+today.getFullYear());
            //Get the selected date (also at midnight)
            var selDate = Date.parse(dateText);

            if(selDate < today) {
                //If the selected date was before today, continue to show the datepicker
                $('#Date').val('');
                $(inst).datepicker('show');
            }
        }
    });
});

基本上,您处理 onSelect 事件。

选择日期后,检查它是否在今天的日期之前。

如果是,那么您立即再次显示日期选择器并清除附加的输入框。


已更新
代码示例现已完全正常运行。这是一个 jsfiddle 来演示。

While I agree that it's weird behavior, you might be able to fake it using the onSelect event of the datepicker.

$(document).ready(function() {
    $('#Date').datepicker({
        onSelect: function(dateText, inst) {
            //Get today's date at midnight
            var today = new Date();
            today = Date.parse(today.getMonth()+1+'/'+today.getDate()+'/'+today.getFullYear());
            //Get the selected date (also at midnight)
            var selDate = Date.parse(dateText);

            if(selDate < today) {
                //If the selected date was before today, continue to show the datepicker
                $('#Date').val('');
                $(inst).datepicker('show');
            }
        }
    });
});

Basically, you handle the onSelect event.

When a date is selected, check to see if it's before today's date.

If it is, then you immediately show the datepicker again and clear out the input box attached to it.


Updated
Code sample is now completely functional. Here's a jsfiddle to demonstrate.

小梨窩很甜 2024-12-06 04:09:51

希望这对某人仍然有帮助。
我的解决方案是对各个日期进行检查,如下所示:

$("#StartDate").datepicker({ dateFormat: 'yy-mm-dd',  beforeShowDay: NotBeforeToday});



function NotBeforeToday(date)
{
    var now = new Date();//this gets the current date and time
    if (date.getFullYear() == now.getFullYear() && date.getMonth() == now.getMonth() && date.getDate() > now.getDate())
        return [true];
    if (date.getFullYear() >= now.getFullYear() && date.getMonth() > now.getMonth())
       return [true];
     if (date.getFullYear() > now.getFullYear())
       return [true];
    return [false];
}

我发现它简单而优雅

Hope this is still helpful to someone.
My solution is to place checks on individual dates like this:

$("#StartDate").datepicker({ dateFormat: 'yy-mm-dd',  beforeShowDay: NotBeforeToday});



function NotBeforeToday(date)
{
    var now = new Date();//this gets the current date and time
    if (date.getFullYear() == now.getFullYear() && date.getMonth() == now.getMonth() && date.getDate() > now.getDate())
        return [true];
    if (date.getFullYear() >= now.getFullYear() && date.getMonth() > now.getMonth())
       return [true];
     if (date.getFullYear() > now.getFullYear())
       return [true];
    return [false];
}

I find it simple and elegant

空气里的味道 2024-12-06 04:09:51

最简单的解决方案可以......

<script type="text/javascript">$('#date').datepicker({ minDate: 0 });</script>

simplest solution could....

<script type="text/javascript">$('#date').datepicker({ minDate: 0 });</script>
冷月断魂刀 2024-12-06 04:09:51

它似乎不可配置。在 jquery.ui.datepicker 的第 1434 行.js,如果指定了 maxDate,它会设置一个 maxDraw 变量并绘制到该日期的月份,而不考虑任何其他旗帜。

但我也同意zzzzBov的观点。为什么用户需要查看他们无法选择的值?

It doesn't appear to be configurable. At line 1434 of jquery.ui.datepicker.js, if a maxDate is specified, it sets a maxDraw variable and draws up to the month of that date, without regard for any other flags.

But I also agree with zzzzBov. Why would the user need to see values they can't select?

梦情居士 2024-12-06 04:09:51

相反,您可以在 .js 文件中编写一个函数,如下所示 -

$(function () {

             var date = new Date();
             var currentMonth = date.getMonth();
             var currentDate = date.getDate();
             var currentYear = date.getFullYear();

            if($("#dateToSelect").length > 0){
                $("#dateToSelect").datepicker({
                 maxDate: new Date(currentYear, currentMonth, currentDate)
             });
            }
})

这里“dateToSelect”是显示日期的字段的 id。此处的日期长度检查不是强制性的。

Instead you can write a function in your .js file like this-

$(function () {

             var date = new Date();
             var currentMonth = date.getMonth();
             var currentDate = date.getDate();
             var currentYear = date.getFullYear();

            if($("#dateToSelect").length > 0){
                $("#dateToSelect").datepicker({
                 maxDate: new Date(currentYear, currentMonth, currentDate)
             });
            }
})

Here "dateToSelect" is the id of the field where the date is shown. The checking of the date length here is not mandatory.

梅窗月明清似水 2024-12-06 04:09:51

你可以简单地这样写

$('.datepicker').datetimepicker({
      format: 'DD-MM-YYYY',
      minDate: Date()
})

You could simply write like this

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