getYearWeek.js 636 B

123456789101112131415161718192021222324
  1. var staticDayTime = require('./staticDayTime')
  2. var toStringDate = require('./toStringDate')
  3. var isValidDate = require('./isValidDate')
  4. /**
  5. * 返回某个年份的第几周
  6. *
  7. * @param {Date} date 日期或数字
  8. * @return {Number}
  9. */
  10. function getYearWeek (date) {
  11. date = toStringDate(date)
  12. if (isValidDate(date)) {
  13. date.setHours(0, 0, 0, 0)
  14. date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7)
  15. var week = new Date(date.getFullYear(), 0, 4)
  16. return Math.round(((date.getTime() - week.getTime()) / staticDayTime + (week.getDay() + 6) % 7 - 3) / 7) + 1
  17. }
  18. return NaN
  19. }
  20. module.exports = getYearWeek