簡體   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