2

Im using the following plugin: FullCalendar.js by : [https://fullcalendar.io/][1]

I am using this with ajax and JSON with an API. however i would like to know if its possible to get the selected day of the week as I want to make certain conditions happen if that day is a Saturday for example:

Is this possible? I have been looking in the docs and came across this:

https://fullcalendar.io/docs/mouse/dayClick/

But im not sure that is correct?

user1673498
  • 431
  • 1
  • 8
  • 26
  • "But im not sure that is correct?" Why not try it and see? Also, depends what you mean by "selected". "Selecting" in fullCalendar is a specific term meaning when the user drags to create an event on a specific time period (https://fullcalendar.io/docs/selection/) - is that what you mean? Or do you mean when the user clicks on a specific day without creating an event (in which case, yes, dayClick). Or do you mean the day(s) which are currently viewable on the calendar? – ADyson May 15 '17 at 14:05
  • Hi should have been a lot clearer. Clicks a specific day without creating an event as im parsing the selected date to make a ajax call. I would use dayClick()? – user1673498 May 15 '17 at 14:16
  • That's the most likely. Like I said, try it and see if it does what you want. Although check whether it still fires if the user clicks on an event within that day. I can't remember if it does or not, and obviously it's up to you whether you want that to happen or not. – ADyson May 15 '17 at 14:36

2 Answers2

2

Per your comment, yes, you would want dayClick() for this:

$('#calendar').fullCalendar({
    dayClick: function(date, jsEvent, view) {
        var day = date.day(); // number 0-6 with Sunday as 0 and Saturday as 6
        alert(day);
    }
});

Working example: https://jsfiddle.net/afn7thme/

Ref. https://fullcalendar.io/docs/mouse/dayClick/

For a full list of all fullcalendar click events, check out the docs at https://fullcalendar.io/docs/mouse/.

user1477388
  • 20,790
  • 32
  • 144
  • 264
  • "If you wanted to prevent the click event from creating an event; you would want to try adding return false; at the end of the callback". There's no indication in the docs that this callback creates an event. I've never noticed that it does? Even the "select" callback doesn't actually render an event by itself. – ADyson May 15 '17 at 14:37
  • From my experience, clicking on a day will create a 24 hour event. If I wanted to prevent this from happening, I might return false on this method. – user1477388 May 15 '17 at 14:39
  • I can't reproduce that at all in a quick demo. All it does is highlight the day using CSS. That's not the same as adding an event. – ADyson May 15 '17 at 14:40
  • Thanks for the answer I have tried this but it returns the date and not the day in a pop up - Clicked on: 2017-05-27 : I need to work out if this is a saturday – user1673498 May 15 '17 at 14:41
  • @user1673498 The example code used here is just from the FC documentation, there's no attempt to customise it for what you need. You need to extract the DOW from it. Since the date is a moment, you can use `date.day()` to discover it. http://momentjs.com/docs/#/get-set/day/ – ADyson May 15 '17 at 14:42
  • You're right, it appears later versions don't create a 24 hour event upon clicking the day. I will update my answer to show you how to get the day. – user1477388 May 15 '17 at 14:46
  • What i have notied is that it has css classes for fc-day-top fc-tue fc-day-top fc-sat did not know this... – user1673498 May 15 '17 at 14:46
  • True, but I wouldn't recommend using that just because you'd have to parse it out. The best bet would be to use the date, get the day like myself and ADyson propose. Good luck! – user1477388 May 15 '17 at 14:55
0

Based on your comments, I think the dayClick callback is the most likely option for you.

$('#calendar').fullCalendar({
    //....
    dayClick: function(date, jsEvent, view) {
     var dow = date.day(); //this will give you the day of the week. Sunday is 0, Saturday is 6.
     //you can now use this to make your ajax call and whatever else you need
    }
});

Note that if there are events in that day, and the user clicks on the actual event (rather than the empty areas around it), the dayClick callback does not fire.

ADyson
  • 57,178
  • 14
  • 51
  • 63