weekOfTheYear.js 502 B

12345678910111213141516
  1. var dayOfTheYear = require('./dayOfTheYear');
  2. /**
  3. * Return the week of the year based on given firstDayOfWeek
  4. */
  5. function weekOfTheYear(date, firstDayOfWeek){
  6. firstDayOfWeek = firstDayOfWeek == null? 0 : firstDayOfWeek;
  7. var doy = dayOfTheYear(date);
  8. var dow = (7 + date.getDay() - firstDayOfWeek) % 7;
  9. var relativeWeekDay = 6 - firstDayOfWeek - dow;
  10. return Math.floor((doy + relativeWeekDay) / 7);
  11. }
  12. module.exports = weekOfTheYear;