检查 pickadate.js 日期选择器是否选择了今天不起作用?

Check if pickadate.js datepicker has selected today not working?

我正在尝试检查此日期选择器 (pickadate.js) 是否已在片场选择了当天。这是我的代码:

var today = new Date();

var tomorrow = new Date();

tomorrow.setDate(today.getDate() + 1);

var nextyear = new Date();

nextyear.setFullYear(nextyear.getFullYear() + 1);

var pickupdatepicker = $("#car-rental-pickup-date").pickadate({
 editable: true,
    format: "mm/dd/yyyy",
    min: today,
    max: nextyear,
    today: "",
    close: "",
    clear: "",
    onSet: function(context) {
    
        var d = new Date(context.select);

        dnotime = new Date(d.toDateString());
        todaynotime =  new Date(today.toDateString());

        var currenthour = new Date().getHours();
        var hourp3 = currenthour + 13;

        console.log (dnotime);
        console.log (todaynotime);

        if (dnotime == todaynotime) {

            time.set({
                disable: [
                { from: [0,0], to: [hourp3,00] }
                ]
            });

            console.log ("today!");

        }else{

            console.log ("not today!");

        }
        
    }
});
<link rel="stylesheet" href="https://amsul.ca/pickadate.js/vendor/pickadate/lib/themes/default.date.css" id="theme_date">


<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://amsul.ca/pickadate.js/vendor/pickadate/lib/picker.js"></script>
<script src="https://amsul.ca/pickadate.js/vendor/pickadate/lib/picker.date.js"></script>
<input type="date" name="car-rental-pickup-date" id="car-rental-pickup-date" class="form-control tleft readonly" value="" placeholder="Select Pickup Date" required>

但是比较两个日期的 if 语句不起作用,但控制台显示它们是相同的。是什么赋予了?有人可以检查一下并告诉我我做错了什么吗?

在您的代码中:

if (dnotime == todaynotime) {

比较两个 Date 对象,所以总是 false。先强制到数字:

if (+dnotime == +todaynotime) {

但是,您可以将其简化为 context.select returns 所选日期的本地开始时间值,因此您可以:

if (context.select == new Date().setHours(0,0,0,0))

并简化前面的代码。这是修改后的原始代码:

var today = new Date();
// Set to start of day
today.setHours(0,0,0,0);
// Copy today as root for tomorrow
var tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 1);
// And for next year
var nextyear = new Date(today);
nextyear.setFullYear(nextyear.getFullYear() + 1);

var pickupdatepicker = $("#car-rental-pickup-date").pickadate({
 editable: true,
    format: "mm/dd/yyyy",
    min: today,
    max: nextyear,
    today: "",
    close: "",
    clear: "",
    onSet: function(context) {
    
        // Could keep value as number, but OK as Date too
        var d = new Date(context.select);

        // This is unnecessary
        // dnotime = new Date(d.toDateString());
        // todaynotime =  new Date(today.toDateString());

        // Not relevant to issue
        // var currenthour = new Date().getHours();
        // var hourp3 = currenthour + 13;

        // Compare time values
        // Could also do: if (+d == + today) {...}
        if (d.getTime() == today.getTime()) {
        /* Not relevant
            time.set({
                disable: [
                { from: [0,0], to: [hourp3,00] }
                ]
            });
        */
            console.log ("today!");

        } else {
            console.log ("not today!");
        }
    }
});
<link rel="stylesheet" href="https://amsul.ca/pickadate.js/vendor/pickadate/lib/themes/default.date.css" id="theme_date">


<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://amsul.ca/pickadate.js/vendor/pickadate/lib/picker.js"></script>
<script src="https://amsul.ca/pickadate.js/vendor/pickadate/lib/picker.date.js"></script>
<input type="date" name="car-rental-pickup-date" id="car-rental-pickup-date" class="form-control tleft readonly" value="" placeholder="Select Pickup Date" required>