0

my requirements is as follows;

If date of the datepicker matches the passed dates using array, then it should show hello on hover over and else show sold. I tried the following code

var rateArray = [new Date(2014, 1, 1), new Date(2014, 1, 2)]; // just some dates.
var date = new Date();
$(document).ready(function () {

    $("#test").datepicker({
        beforeShowDay: function (date) {
            if ($.inArray(date, rateArray)) {
                return [true, 'tiptip', 'hello'];
            }
            else {
                return [false, 'tiptip', 'Sold'];
            }
        }


    });

});

But it is not working, it is showing "hello" on every date. If anybody know the solution then please help, thanks.

2 Answers 2

1

Try

//use a string representation for the date
var rateArray = ['01 Jan 2014', '02 Jan 2014']; // just some dates.
$(document).ready(function () {
    $("#test").datepicker({
        beforeShowDay: function (date) {
            //convert the date to a string format same as the one used in the array
            var string = $.datepicker.formatDate('dd M yy', date)
            // $.inArray() returns -1 if the item is not found so change the condition accordingly
            if ($.inArray(string, rateArray) > -1) {
                return [true, 'tiptip', 'hello'];
            } else {
                return [false, 'tiptip', 'Sold'];
            }
        }
    });
});

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

4 Comments

And remember JS Months start at 0 - so he had to look at Feb
@ArunPJohny Thank you very much brother, fabulous. Just one more query, I want to pass this array of dates from ".cs' page to the "script" page, I don't know how to achieve that. Help me if you can.
@Learner what is the server side technology used?
@ArunPJohny Asp.Net MVC 4.0 using c#.
0

Inspired by a now deleted answer:

Live Demo

var dates = {};
dates[new Date('02/13/2014').getTime()]='Holiday1';
dates[new Date('02/14/2014').getTime()]='Holiday2';
dates[new Date('02/15/2014').getTime()]='Holiday3';

$('#txtDate').datepicker({
    beforeShowDay: function(date) {
      var hlText = dates[date.getTime()];
      return (hlText)?[true, "tiptip", hlText]: [true, '', ''];
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.