简体   繁体   English

是否可以在 Tempus Dominus bootstrap 4 datetimepicker 中将 minDate 和 maxDate 设置为同一天?

[英]Is it possible to set minDate and maxDate to the same day in Tempus Dominus bootstrap 4 datetimepicker?

i'm using TempusDominus Bootstrap4 DateTime Picker ( https://tempusdominus.github.io/bootstrap-4/ ).我正在使用 TempusDominus Bootstrap4 DateTime Picker ( https://tempusdominus.github.io/bootstrap-4/ )。 I only have 2 simple inputs in HTML which are these我在 HTML 中只有 2 个简单的输入,它们是这些

<div class='mb-3'>
     <input type="text" class="form-control datetimepicker-input" id="datetimepicker7" data-toggle="datetimepicker" data-target="#datetimepicker7" readonly="readonly"/>
</div>
<div class='mb-3'>      
     <input type="text" class="form-control datetimepicker-input" id="datetimepicker8" data-toggle="datetimepicker" data-target="#datetimepicker8" readonly="readonly"/>
</div>    

and this in jquery这在jquery中

  var $horaInicio = $('#datetimepicker7');
  var $horaFin = $('#datetimepicker8');

  $horaInicio.datetimepicker({
    useCurrent: false,
    locale:'es',
    daysOfWeekDisabled: [0,6],
    minDate: moment(),
    autoClose: true,
    ignoreReadonly: true,

  });

  $horaFin.datetimepicker({
    useCurrent: false,
    locale: 'es',
    daysOfWeekDisabled: [0,6],
    ignoreReadonly: true,
    minDate: moment(),
    autoClose: true,

  });

  $horaInicio.on("change.datetimepicker", function (e) {

    $horaFin.datetimepicker('minDate',e.date);

  });


  $horaFin.on("change.datetimepicker", function (e) {
    $horaInicio.datetimepicker('maxDate', e.date);
  });

In my system i need to manage reservations that should start and finish the same day, so i want to limit the second datepicker (#datepicker8) so the minDate property is set to the same date selected in the first datepicker (#datepicker7) and the maxDate property to 1 day more.在我的系统中,我需要管理应该在同一天开始和结束的预订,因此我想限制第二个日期选择器 (#datepicker8),以便将 minDate 属性设置为在第一个日期选择器 (#datepicker7) 和maxDate 属性增加了 1 天。 (ie, all days disabled except the one picked with the first datetimepicker) (即,除了第一个日期时间选择器选择的那一天之外,所有日子都被禁用)

With the code above i can pick as start day "today" and then i'm able to select any day from "today" to years to come.使用上面的代码,我可以选择“今天”作为开始日,然后我可以选择从“今天”到未来几年的任何一天。 If i select first let's say (DD/MM/YYYY) 11/09/2019 14:00 i can then select 11/09/2019 14:30 or 15:00 with no problems.如果我先选择 (DD/MM/YYYY) 11/09/2019 14:00 然后我可以选择 11/09/2019 14:30 或 15:00 没有问题。 But when i try to modify the maxDate of the 2nd datepicker it doesn't works但是当我尝试修改第二个日期选择器的 maxDate 时它不起作用

I already try this:我已经尝试过这个:

$horaInicio.on("change.datetimepicker", function (e) {

    $horaFin.datetimepicker('minDate',e.date);
    $horaFin.datetimepicker('maxDate', e.date.add(1,'day');

  });

but it doesn't work, i cannot select any day with the 2nd datetimepicker.但它不起作用,我无法使用第二个日期时间选择器选择任何一天。 Since i add 1 day from today to the maxDate, that day appears as enabled but i cannot select it, clicking it does nothing.由于我从今天起将 1 天添加到 maxDate,那一天显示为已启用但我无法选择它,单击它什么也不做。 I have no errors showing up.我没有出现错误。 It seems like setting minDate and maxDate brokes the datepicker.似乎设置 minDate 和 maxDate 破坏了日期选择器。

Check out this: How to allow maxDate and minDate to be equal看看这个: 如何允许 maxDate 和 minDate 相等

I would say that this is probably also a date format issue.我想说这可能也是一个日期格式问题。
Try formatting the date using:尝试使用以下格式格式化日期:

function formatDate(date) {
    var months = [
        "1", "2", "3",
        "4", "5", "6",
        "7", "8", "9",
        "10", "11", "12"
    ];

    var day = date.getDate();
    var monthIndex = date.getMonth();
    var year = date.getFullYear();

    return day + '/' + months[monthIndex] + '/' + year;
}

Then set the formatted date as parameters to minDate and maxDate functions:然后将格式化日期设置为 minDate 和 maxDate 函数的参数:

$horaInicio.on("change.datetimepicker", function (e) {
    let formattedDate = formatDate(e.date.toDate());
    $horaFin.datetimepicker('minDate', formattedDate);
});


$horaFin.on("change.datetimepicker", function (e) {
    let formattedDate = formatDate(e.date.toDate());
    $horaInicio.datetimepicker('maxDate', formattedDate);
});

Or even maybe:或者甚至可能:

$horaInicio.on("dp.change", function (e) {
    let formattedDate = formatDate(e.date.toDate());
    $horaFin.data("DateTimePicker").minDate(formattedDate);
});


$horaFin.on("dp.change", function (e) {
    let formattedDate = formatDate(e.date.toDate());
    $horaInicio.data("DateTimePicker").maxDate(formattedDate);
});

Ok, i could resolve it, for sure there is a more elegant solution but for anyone there having the same problem: when the date changes in e.date.add() method, it changes all the references, so doing minDate: e.date and then maxDate: e.date.add(1,"day") sets minDate and maxDate to the exact same date.好的,我可以解决它,当然有一个更优雅的解决方案,但对于任何有同样问题的人:当 e.date.add() 方法中的日期更改时,它会更改所有引用,因此执行 minDate: e. date 和 maxDate: e.date.add(1,"day") 将 minDate 和 maxDate 设置为完全相同的日期。

I solved it with a clone() and then destroying the datepicker and re-initiating it我用 clone() 解决了它,然后销毁了日期选择器并重新启动它

    $horaInicio.on("change.datetimepicker", function(e) {
        let fecha = e.date.clone();

        $horaFin.datetimepicker("destroy");
        $horaFin.datetimepicker({
            useCurrent: false,
            locale: "es",
            daysOfWeekDisabled: [0, 6],
            ignoreReadonly: true,
            minDate: fecha,
            maxDate: e.date.add(1, "day"),
            autoClose: true
        });
    });

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

相关问题 如何在更改时为引导程序datetimepicker设置minDate和maxDate - How to set minDate and maxDate for bootstrap datetimepicker on change 为 Tempus Dominus Bootstrap 4 设置语言环境 - Set locale for Tempus Dominus Bootstrap 4 Bootstrap v4 datetimepicker 不工作(Tempus Dominus) - Bootstrap v4 datetimepicker is not working (Tempus Dominus) jQuery datetimepicker异常日,带有minDate和maxDate - JQuery datetimepicker exception day with minDate and maxDate 如何在 Bootstrap DateTimePicker 上允许 maxDate 和 minDate 相等? - How to allow maxDate and minDate to be equal on Bootstrap DateTimePicker? 可观察到的淘汰赛中的datetimepicker引导程序minDate和maxDate - datetimepicker bootstrap minDate and maxDate in observable knockout 有没有办法更改为 tempus dominus datetimepicker 设置的图标? - Is there a way to change what icon set for tempus dominus datetimepicker? Select 仅来自 UI 的月份和日期。如何从 DateTimePicker 中隐藏年份 - Tempus Dominus Datetime Picker Bootstrap - Select Month and Day only from UI .How to hide Year from DateTimePicker- Tempus Dominus Datetime Picker Bootstrap 用于 bootstrap 5 的 Tempus Dominus 安装 - Tempus Dominus installation for bootstrap 5 DateTimePicker:函数从minDate maxDate添加删除日期 - DateTimePicker: function add remove day from minDate maxDate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM