Show week number in FullCalendar

后端 未结 3 1605
星月不相逢
星月不相逢 2021-01-25 04:09

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 tit

相关标签:
3条回答
  • 2021-01-25 04:39

    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
    0 讨论(0)
  • 2021-01-25 04:55

    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.

    0 讨论(0)
  • 2021-01-25 04:58

    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!

    0 讨论(0)
提交回复
热议问题