[英]Why does datepicker highlight not work when I use “==”?
.Highlighted a{
background-color : Green !important;
background-image :none !important;
color: White !important;
font-weight:bold !important;
font-size: 9pt;
}
$(document).ready(function () {
var date1 = new Date(2014, 5, 6);
var date2 = new Date(2014, 5, 17);
$('#datepicker').datepicker({
dateFormat: "mm/dd/yy",
beforeShowDay: function (date) {
if (date == date1 ) {
return [true, 'Highlighted', 'Available Date'];
}
return [false, '', ''];
}
});
});
This one doesn't work, because of date==date1
. 这个不起作用,因为
date==date1
。 If I change it to date<=date1
, it works fine. 如果我将它更改为
date<=date1
,它可以正常工作。 I thought javascript is a weakly typed language and it compares the content, rather than reference. 我认为javascript是一种弱类型语言,它比较内容,而不是引用。 I don't wanna do something like
(date.getDay==date1.getDay &&....)
. 我不想做
(date.getDay==date1.getDay &&....)
。 Is there an easier way to compare the values? 是否有更简单的方法来比较这些值?
Use the +
unary operator ( reference ) to convert the values to numerics for comparison. 使用
+
一元运算符( 引用 )将值转换为数值以进行比较。
The unary + operator converts its operand to Number type.
一元+运算符将其操作数转换为数字类型。
if (+date === +date1 ) {
return [true, 'Highlighted', 'Available Date'];
}
OR 要么
if (!(date - date1)) {
return [true, 'Highlighted', 'Available Date'];
}
正如本文所述,您希望使用以下内容来比较日期:
date.getTime() == date1.getTime()
其他方式:
if (date.valueOf() == date1.valueOf())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.