繁体   English   中英

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

[英]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 对象,所以它总是假的。 先强制编号:

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>

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM