I make the schedule of classes. It is necessary that the calendar week numbers are displayed not from the beginning of the year, but from the beginning of September. The documentation says

You can also specify a week number.

I am writing a function() { var week = moment().week()-35; return week; } function() { var week = moment().week()-35; return week; } function() { var week = moment().week()-35; return week; } does not work, weeks are displayed from the beginning of the year, just moment().week()-35; same story.

Is there any working example function for the weekNumberCalculation? I use the FullCalendar extension for Yii, but the essence does not change.

UPD. figured out the function, the numbers of weeks have changed, but now they are all the same)

UPD2. Understood. The selected date is passed to the function. The code is:

 'weekNumberCalculation'=>new CJavaScriptExpression("function getWeekNumber(d) { d = new Date(+d); // Начало семестра var yearStart = new Date(d.getFullYear(),8,1); var weekNo = Math.ceil(1+(( (d - yearStart) /86400000))/7); if (weekNo < 0) { return 0 } else {return weekNo}; }"), 

    2 answers 2

    I could be wrong, but it’s written in the documentation itself (the link is the same as your question), that when creating the moment, you can “explicitly” indicate the date ie

     moment('2016-09-01'); 

    Therefore, you need to put down the correct date inside the moment and remove -35 (I did not quite understand why it was needed).

    Update Here is a piece of fullCalendar source code. As I understand it, you can simply return the desired number in the function for your purposes. // assign a normalized value, to be used by our .week() moment extension localeData._fullCalendar_weekCalc = (function(weekCalc) { if (typeof weekCalc === 'function') { return weekCalc; } else if (weekCalc === 'local') { return weekCalc; } else if (weekCalc === 'iso' || weekCalc === 'ISO') { return 'ISO'; } })(options.weekNumberCalculation);

    I would try like this ... .weekNumberCalculation(function(){ var local = $.fullCalendar.moment('2014-05-01T12:00:00'); return local.week()-35; // нужно смотреть значение week тут });

    I tried to do it in jsfiddle but he told me that there is an error in the plug-in lib ... therefore I can not vouch for the correctness.

    • -35 is needed to calculate the desired school week, for example, 38 week in a year is 3 week in a academic year - Roman Dakhin
    • Unfortunately, your method didn’t work either - Roman Dakhin
    • those. here is a declaration of the moment var local = $.fullCalendar.moment('2014-05-01T12:00:00'); does not return the desired week? Strange - alexoander
    • Maybe because I use the FullCalendar extension for Yii? Could you please indicate in full what you need to write in weekNumberCalculation: - Roman Dakhin
    • Updated the answer - mb will help - alexoander
     'weekNumberCalculation'=>new CJavaScriptExpression("function getWeekNumber(d) { d = new Date(+d); // Начало семестра var yearStart = new Date(d.getFullYear(),8,1); var weekNo = Math.ceil(1+(( (d - yearStart) /86400000))/7); if (weekNo < 0) { return 0 } else {return weekNo}; }"),