getMonthWeek.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. var staticWeekTime = require('./staticWeekTime')
  2. var staticStrFirst = require('./staticStrFirst')
  3. var helperGetYMDTime = require('./helperGetYMDTime')
  4. var getWhatMonth = require('./getWhatMonth')
  5. var toStringDate = require('./toStringDate')
  6. var getWhatWeek = require('./getWhatWeek')
  7. var isValidDate = require('./isValidDate')
  8. /**
  9. * 返回某个月的第几周
  10. *
  11. * @param {Date} date 日期或数字
  12. * @return {Number}
  13. */
  14. function getMonthWeek (date) {
  15. var monthFirst, monthFirstWeek
  16. var currentDate = toStringDate(date)
  17. if (isValidDate(currentDate)) {
  18. monthFirst = getWhatMonth(currentDate, 0, staticStrFirst)
  19. monthFirstWeek = getWhatWeek(monthFirst, 0, 1)
  20. if (monthFirstWeek < monthFirst) {
  21. monthFirstWeek = getWhatWeek(monthFirst, 1, 1)
  22. }
  23. if (currentDate >= monthFirstWeek) {
  24. return Math.floor((helperGetYMDTime(currentDate) - helperGetYMDTime(monthFirstWeek)) / staticWeekTime) + 1
  25. }
  26. return getMonthWeek(getWhatWeek(currentDate, 0, 1))
  27. }
  28. return NaN
  29. }
  30. module.exports = getMonthWeek