getWhatWeek.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. var staticDayTime = require('./staticDayTime')
  2. var staticWeekTime = require('./staticWeekTime')
  3. var staticParseInt = require('./staticParseInt')
  4. var helperGetDateTime = require('./helperGetDateTime')
  5. var toStringDate = require('./toStringDate')
  6. var isValidDate = require('./isValidDate')
  7. /**
  8. * 返回前几周或后几周的星期几
  9. *
  10. * @param {Date} date 日期
  11. * @param {Number} week 周(默认当前周)、前几周、后几周
  12. * @param {Number} day 星期天(默认0)、星期一(1)、星期二(2)、星期三(3)、星期四(4)、星期五(5)、星期六(6)
  13. * @return {Date}
  14. */
  15. function getWhatWeek (date, week, day) {
  16. var time, whatDayTime, currentDay, customDay
  17. date = toStringDate(date)
  18. if (isValidDate(date)) {
  19. customDay = staticParseInt(/^[0-7]$/.test(day) ? day : date.getDay())
  20. currentDay = date.getDay()
  21. time = helperGetDateTime(date)
  22. whatDayTime = time + ((customDay === 0 ? 7 : customDay) - (currentDay === 0 ? 7 : currentDay)) * staticDayTime
  23. if (week && !isNaN(week)) {
  24. whatDayTime += week * staticWeekTime
  25. }
  26. return new Date(whatDayTime)
  27. }
  28. return date
  29. }
  30. module.exports = getWhatWeek