简体   繁体   中英

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 . If I change it to date<=date1 , it works fine. I thought javascript is a weakly typed language and it compares the content, rather than reference. I don't wanna do something like (date.getDay==date1.getDay &&....) . Is there an easier way to compare the values?

Demo Fiddle

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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