getWhatDay.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. var staticStrFirst = require('./staticStrFirst')
  2. var staticStrLast = require('./staticStrLast')
  3. var staticParseInt = require('./staticParseInt')
  4. var helperGetDateFullYear = require('./helperGetDateFullYear')
  5. var helperGetDateMonth = require('./helperGetDateMonth')
  6. var helperGetDateTime = require('./helperGetDateTime')
  7. var toStringDate = require('./toStringDate')
  8. var isValidDate = require('./isValidDate')
  9. /**
  10. * 返回前几天或后几天的日期
  11. *
  12. * @param {Date} date 日期或数字
  13. * @param {Number} day 天(默认当天)、前几天、后几天
  14. * @param {String} mode 获取时分秒(null默认当前时分秒)、日初(first)、日末(last)
  15. * @return {Date}
  16. */
  17. function getWhatDay (date, day, mode) {
  18. date = toStringDate(date)
  19. if (isValidDate(date) && !isNaN(day)) {
  20. date.setDate(date.getDate() + staticParseInt(day))
  21. if (mode === staticStrFirst) {
  22. return new Date(helperGetDateFullYear(date), helperGetDateMonth(date), date.getDate())
  23. } else if (mode === staticStrLast) {
  24. return new Date(helperGetDateTime(getWhatDay(date, 1, staticStrFirst)) - 1)
  25. }
  26. }
  27. return date
  28. }
  29. module.exports = getWhatDay