gpt4 book ai didi

javascript - 检查 pickadate.js datepicker 选择的今天是否不起作用?

转载 作者:行者123 更新时间:2023-12-02 21:36:35 24 4
gpt4 key购买 nike

我正在尝试检查此日期选择器 (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 返回所选日期的本地开始时间值,因此您可以执行以下操作:

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>

关于javascript - 检查 pickadate.js datepicker 选择的今天是否不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60476875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com