Fullcalendar: Change the color for specific days
Asked Answered
G

8

17

I'm stuck with a project I get at work. I need to change the background color of some days. It's a calendar where the user should see, which days are available and which not. I found out that there is an attribute called "data-date", but I didn't found a possibility to use it.

Is there any way to manipulate the background of specific days?

I think there must be a way, cause the cell which shows the current date has another color too.

Gratulate answered 12/7, 2013 at 11:9 Comment(0)
G
39

For the views month, basicWeek and basicDay you can change the rendering of the days by providing a dayRender function. E.g.:

$("#calendar").fullCalendar({
    dayRender: function (date, cell) {
        cell.css("background-color", "red");
    }
});

The documentation for dayRender is available here: http://arshaw.com/fullcalendar/docs/display/dayRender/

And here's a working example on jsfiddle: http://jsfiddle.net/kvakulo/CYnJY/4/

Galvez answered 12/7, 2013 at 12:34 Comment(2)
See my answer below for a CSS based solution which presumably would be faster: https://mcmap.net/q/686213/-fullcalendar-change-the-color-for-specific-daysAp
I can only pass date here how can I pass it with the month and year like [10-12-2018,24-12-2018] instead of [10,24]Lilas
A
9

For those looking to simply change the color of past dates, target .fc-past in your css and add a background-color property. E.g.,:

.fc-past {
    background-color: silver;
}
Ap answered 29/4, 2016 at 0:12 Comment(0)
K
8
    dayRender: function(date, cell){
        if (moment().diff(date,'days') > 0){
            cell.css("background-color","silver");
        }
    },
Kantos answered 21/10, 2014 at 17:50 Comment(0)
I
5

If any one want to jquery fullcalendar previous all day red(or any other) colored then this is the code.

    var $calendar = $('#calendar').fullCalendar({
    header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,basicWeek,basicDay'
    },

    defaultView: 'month',

    dayRender: function (date, cell) {
       var check = $.fullCalendar.formatDate(date,'yyyy-MM-dd');
                    var today = $.fullCalendar.formatDate(new Date(),'yyyy-MM-dd');
                    if (check < today) {
                        cell.css("background-color", "red");
                    }
    }
});

Regin Larsen code help me achive this. Thanks Regin Larsen.

Inflow answered 9/1, 2014 at 12:54 Comment(0)
W
4

Why not using the "data-date" attribute?

$("#calendar").fullCalendar(function() {

  viewRender: function() {
    $("[data-date="+$.fullCalendar.formatDate(new Date(), "yyyy-MM-dd")+"]").css("background-color", "red");
},

....
Warmhearted answered 11/1, 2014 at 1:12 Comment(1)
this one helped me. because dayRender method doesn't work properly with a dayThreeViewTomika
G
1

When working with external libraries, you should try not to take advantage of anything that was generated by the library. Since in the next version, if they change the way the library works internally, the library will still be backward compatible but your code will stop working. So try to use the library API as much as possible instead of doing hacks.

Answering your question, one way to do it will be, add a new event to all the days that are not available. This can be done by creating event object and doing a renderEvent(.fullCalendar( 'renderEvent', event [, stick ] )). While creating an event object assign the background color as the color you want and set the color, text color, border color to the same as background if you dont want it to be visible.

Edit: Regin Larsen's answer seems better. I didn't notice that in the documentation.

Gaggle answered 12/7, 2013 at 12:39 Comment(0)
G
1

To compare using the date parameter for a specific day:

$("#calendar").fullCalendar({
    dayRender: function (date, cell) {
        if (date.isSame('2016-08-04')) {
           cell.css("background-color","red");
        }
    }
});

isSame comes from moment.js, which is used by fullcalendar: http://momentjs.com/docs/#/query/is-same/

Goober answered 4/8, 2016 at 14:15 Comment(0)
A
0

getDate does not work I guess, use moment instead.

inside var calendar = $('#calendar').fullCalendar({ ... }

dayRender: function (date, cell) {
        var today = moment('2018-06-22T00:00Z');
        if (date.isSame(today, "day")) {
            cell.css("background-color", "blue");
        }
    },
Attwood answered 3/6, 2018 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.