Show week number in FullCalendar

情到浓时终转凉″ 提交于 2019-12-02 20:06:39

问题


I'm looking for a way to show the week number on the agendaWeek view. I've tried this method but without result.

Actually, I need to put the number in the calendar title like this

titleFormat:
        {
            month: "'Calendar<br />'MMMM yyyy",
            week: "'Calendar<br />Week' W",
            day: "'Calendar<br />'dddd dd MMM yyyy"
        }

But of course 'W' doesn't exist, is there a way to do that ?

Thanks.


回答1:


You can turn on week numbers in FullCalendar with this option:

$("#calendar").fullCalendar({
  weekNumbers: true
});

Here's a link to the relevant documentation: http://arshaw.com/fullcalendar/docs/display/weekNumbers/

Btw, if I use "W" in title format, then the current week number is inserted (both in month, week and day). Are you using the latest version of FullCalendar? I'm using v. 1.6.1.

  • Regin



回答2:


i have used this code while ago:

<script type="text/javascript" language="JavaScript">
function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function getWeek(year,month,day) {
    var when = new Date(year,month,day);
    var newYear = new Date(year,0,1);
    var offset = 7 + 1 - newYear.getDay();
    if (offset == 8) offset = 1;
    var daynum = ((Date.UTC(y2k(year),when.getMonth(),when.getDate(),0,0,0) - Date.UTC(y2k(year),0,1,0,0,0)) /1000/60/60/24) + 1;
    var weeknum = Math.floor((daynum-offset+7)/7);
    if (weeknum == 0) {
        year--;
        var prevNewYear = new Date(year,0,1);
        var prevOffset = 7 + 1 - prevNewYear.getDay();
        if (prevOffset == 2 || prevOffset == 8) weeknum = 53; else weeknum = 52;
    }
    return weeknum;
}

var now = new Date();
alert('Week number = ' + getWeek(y2k(now.getYear()),now.getMonth(),now.getDate()));
</script>

its pretty simple, the getWeek() function gets year, month and day, and returns the week number.

DEMO

Hope that what you wished for.

GoodLuck!




回答3:


I found a way to do this and it works for me :

Place this in the settings :

viewDisplay: function(view) {
      if (view.name == 'agendaWeek')
      {
          $("table.fc-header span.fc-header-title h2").append("<br />Week " + getWeekNumber(view.start));
      }
   }, // others settings...

Place this in the same file, before the fullcalendar settings

function getWeekNumber(d) {
  d = new Date(d);
  d.setHours(0, 0, 0);

  d.setDate(d.getDate() + 4 - (d.getDay() || 7));

  const yearStart = new Date(d.getFullYear(), 0, 1);

  const weekNo = Math.ceil(((d - yearStart) / 86400000 + 1) / 7);

  return weekNo;
}

And that's that. Thank you for your previous answer, I'm going to try to update my fullcalendar version.



来源:https://stackoverflow.com/questions/17474557/show-week-number-in-fullcalendar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!