5
   .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?

Shaunak D
  • 20,588
  • 10
  • 46
  • 79
An Overflowed Stack
  • 334
  • 1
  • 5
  • 20
  • possible duplicate of [Compare dates with JavaScript](http://stackoverflow.com/questions/492994/compare-dates-with-javascript) – Rodney G Jun 20 '14 at 16:50

3 Answers3

5

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'];
}
Shaunak D
  • 20,588
  • 10
  • 46
  • 79
  • This essentially does what the other answer does, however I find the other answer easier to read. Plus, someone new to JavaScript wouldn't know what the heck is being done with the plusses. – j08691 Jun 20 '14 at 16:57
  • @j08691, i know, its just that I had solved the same problem using '+' some time before. – Shaunak D Jun 20 '14 at 16:58
  • 1
    @j08691 I'd argue that someone new to javascript isn't going to look at `getTime()` and know that it means _"seconds since 1970-01-01 UTC"_ rather than _"the time portion of a date"_. `valueOf()` seems more attractive... but still not great. – canon Jun 20 '14 at 17:13
3

As suggested in this post you want to use the following to compare dates:

date.getTime() == date1.getTime()
Community
  • 1
  • 1
ebo
  • 2,717
  • 1
  • 27
  • 22
2

Another way:

if (date.valueOf() == date1.valueOf())
James
  • 20,957
  • 5
  • 26
  • 41