4

I'm using the jQuery UI datepicker to allow the user to select a date. I need to color 7 days after the selected date.

For example, if the user has selected 1.1.2015, the days 2.1.2015 to 8.1.2015 shall be colored upon click (on the 1.1.2015). I was following this guide but I am not able to make this work.

Basically what I'm doing is creating an array of future dates (based on the calculation of milliseconds from the selected date + 86400000 milliseconds for each day), and then trying to apply css class on this array. Please advise.

EDIT:
Maybe I should have mentioned, but this picker is inline so the changes must take place instantly.

EDIT2:
Here's an example via jsfiddle.

JS:

var arrayOfFollowingWeekDates = [];
var selectedStartingDate;

//date picker configuration
    $('#datepicker').datepicker({
        onSelect: function(dateText, inst){

            selectedStartingDate = dateText;
            var selectedDateAsObject = $(this).datepicker('getDate');
            arrayOfFollowingWeekDates = calcFollowingtWeekDates(selectedDateAsObject);
            if(selectedDateAsObject > new Date){
                console.log(selectedStartingDate);

            }else{
                console.log("bad date.");
            }
        },
        inline: true,
        showOtherMonths: true,
        beforeShowDay: function(day, date){
            var day = day.getDay();
            if (day == 5 || day == 6){
                return [false, ""];
            } else {
                return [true, ""];
            }
            var highlight = arrayOfFollowingWeekDates[date];
            if (highlight) {
                 return [true, "event", highlight];
            } else {
                 return [true, '', ''];
            }
        }
    });

//this creates an array of the desired week, based on date objects
    function calcFollowingtWeekDates(selectedDateObj){
        var followingWeek = [];
        var tempArrayOfNextDates = [];
        var selectedDateInMilliseconds = selectedDateObj.getTime();
        console.log("selected date in milliseconds: "+selectedDateInMilliseconds);
        tempArrayOfNextDates[0]=selectedDateInMilliseconds;
        var day;
        var prevDay=selectedDateInMilliseconds;
        for(var i=0;i<7;i++){
            tempArrayOfNextDates[i] = 86400000 + prevDay;
            day = new Date(tempArrayOfNextDates[i]);
            prevDay = tempArrayOfNextDates[i];
            followingWeek[i]=day;
            console.log("next day is : "+ day);
        }
        return followingWeek;
    }

CSS:

.event a {
    background-color: #42B373 !important;
    background-image :none !important;
    color: #ffffff !important;
}
2
  • 1
    1st: your code has errors, 2nd: please provide us a link to a "working" fiddle. You will get more response if you do so.
    – ggzone
    Commented May 26, 2015 at 14:28
  • 1
    Thanks, I've updated my post with a jsfiddle.
    – Alex
    Commented May 26, 2015 at 14:37

1 Answer 1

0

Here is a working fiddle: http://jsfiddle.net/97L93a3h/2/

var selectedDay = new Date().getTime();

$('.datepicker').datepicker({
    onSelect: function (date) {
        selectedDay = $(this).datepicker('getDate').getTime();
    },
    beforeShowDay: function (date) {
        var d = date.getTime();
        if (d > selectedDay && d < selectedDay + 1 + 86400000 * 7) {
            return [true, 'ui-state-highlight', ''];
        } else {
            return [true, ''];
        }
    }
});

Merge this method into your existing script.

Not the answer you're looking for? Browse other questions tagged or ask your own question.