Skip to content

Instantly share code, notes, and snippets.

@jboesch
Created May 20, 2011 17:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jboesch/983335 to your computer and use it in GitHub Desktop.
Save jboesch/983335 to your computer and use it in GitHub Desktop.
Get the date by clicking a month td cell in $.fullCalendar (http://arshaw.com/fullcalendar/)
/**
* Get the date we just clicked on (td cell)
*
* @param {Object} $td The jQuery td element
*/
var getDateFromTdCell = function($td)
{
var self = this;
var $tr = $td.parent();
var date_obj = self.el.fullCalendar('getDate');
var current_date = parseInt($td.find('.fc-day-number').text());
var current_month = date_obj.getMonth();
var current_year = date_obj.getFullYear();
// We might click on a date from a previous month or next month (grey'd out)
var is_previous_month = ($tr.hasClass('fc-first') && $td.hasClass('fc-other-month'));
var is_next_month = (!is_previous_month && $tr.hasClass('fc-last') && $td.hasClass('fc-other-month'));
if(is_previous_month)
{
// If we're on January and we went to a previous month
if(current_month == 0)
{
current_month = 11; // December
current_year--;
}
else
{
current_month--;
}
}
else if(is_next_month)
{
if(current_month == 11)
{
current_month = 0;
current_year++;
}
else
{
current_month++;
}
}
var final_date = new Date(current_year, current_month, current_date);
return final_date;
}
@sgreenfield
Copy link

I was doing something very similar to this gist until I discovered that we can access an internal function to get the date from the cell like this:

function getDateFromCell(td, calInstance){
  var cellPos = {
      row: td.parents('tbody').children().index(td.parent()),
      col: td.parent().children().index(td)
  };

  return calInstance.fullCalendar('getView').cellDate(cellPos);
}

We can also do the reverse with this:

function getCellFromDate(thisDate, calInstance){
  var coords = calInstance.fullCalendar('getView').dateCell(new Date(thisDate)),
        $row = calInstance.find('.fc-view-month tbody tr').eq(coords.row),
        $cell = $row.find('td').eq(coords.col);

  return $cell;
}

@wkrsz
Copy link

wkrsz commented May 29, 2013

$(cell).data('date');

– cells have data-date attribute on them, AFAIR.

@dvliman
Copy link

dvliman commented Jun 15, 2013

@WojtekKruszewski thanks for data('date'). much cleaner solution to use

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment