如何在Bootstrap DateTimePicker上允许maxDate和minDate相等? [英] How to allow maxDate and minDate to be equal on Bootstrap DateTimePicker?

查看:291
本文介绍了如何在Bootstrap DateTimePicker上允许maxDate和minDate相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了创建报告,我需要能够选择相同的开始日期和结束日期.
我能够做到这一点,但是只有在我第一次选择maxDate时才可以.
之后,我无法选择与maxDate相同的minDate值.

In order to create a report, I need to be able to choose the same starting and ending date.
I'm able to do that, but only on the first time that I pick the maxDate.
After that I can't pick the same value of the minDate as maxDate.

例如:
浏览器打开.
我选择2020年1月1日作为开始日期(minDate).
我选择2020年1月1日作为结束日期(maxDate).
我将结束日期更改为02/01/2020.
现在,我无法再次选择"01/01/2020"作为结束日期.

E.g:
Browser opens up.
I choose 01/01/2020 as starting date (minDate).
I choose 01/01/2020 as ending date (maxDate).
I change ending date to 02/01/2020.
Now I'm unable to pick '01/01/2020' as ending date again.

我需要能够为minDate和maxDate选择相同的时刻
我似乎在DateTimePicker文档中找不到解决方案

I need to be able to select the same moment for minDate and maxDate
I can't seem to find a solution for this in the DateTimePicker documentation

我尝试在change事件上设置一些值:

I've tried setting some values on the change event:

$(".datetimepickerTo").on("dp.change", function (e) {
    $('.datetimepickerFrom').data("DateTimePicker").maxDate(e.date);
});

在datetimepicker选择器上:

And on the datetimepicker selector:

$('.datetimepickerTo').datetimepicker({
    useCurrent: false
});

推荐答案

原来是日期格式问题.
由于某些原因,第一次选择日期时,日期输出为:

Turns out it was a date format issue.
For some reason when selecting dates for the first time, the date output was:

'2020年1月1日星期三格林尼治标准时间0300(巴西利亚利亚·帕拉朗)

'Wed Jan 01 2020 16:39:34 GMT-0300 (Horário Padrão de Brasília)'.

我选择日期的实际时间是"16:39:34".

Where '16:39:34' was the actual time that I picked the date.

尽管第二次选择,输出为:

Although, on the second time selecting, the output was:

'2020年1月1日,星期三00:00:00 GMT-0300(巴西利亚霍拉里奥帕德朗)'

'Wed Jan 01 2020 00:00:00 GMT-0300 (Horário Padrão de Brasília)'

确定两个不同的日期.

我的方法是将日期格式化为所需的格式.使用此功能:

My approach was to format the date to the format that I needed. Using this function:

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;
}

输出为:

2020年1月1日

01/01/2020

然后我只需要将格式化的日期传递给minDate()和maxDate()函数:

Then I just had to pass the formatted date to minDate() and maxDate() functions:

$(".datetimepickerFrom").datetimepicker().on("dp.change", function (e) {
    const formattedDate = formatDate(e.date.toDate())
    $('.datetimepickerTo').data("DateTimePicker").minDate(formattedDate);
});

$(".datetimepickerTo").datetimepicker().on("dp.change", function (e) {
    const formattedDate = formatDate(e.date.toDate())
    $('.datetimepickerFrom').data("DateTimePicker").maxDate(formattedDate);
});

这篇关于如何在Bootstrap DateTimePicker上允许maxDate和minDate相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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