UNPKG

1.08 kBJavaScriptView Raw
1var getWhatMonth = require('./getWhatMonth')
2
3var toStringDate = require('./toStringDate')
4
5var isValidDate = require('./isValidDate')
6
7function getQuarterNumber (date) {
8 var month = date.getMonth()
9 if (month < 3) {
10 return 1
11 } else if (month < 6) {
12 return 2
13 } else if (month < 9) {
14 return 3
15 }
16 return 4
17}
18
19/**
20 * 返回前几季度或后几季度的日期
21 *
22 * @param {Date} date 日期
23 * @param {Number} offset 季度(默认当前季度)、前几季度、后几季度
24 * @param {Number} day 获取哪天:月初(first)、月末(last)、指定天数(数值),如果为空,但超过指定月份的天数时,则默认单月最后一天
25 * @return {Date}
26 */
27function getWhatQuarter (date, offset, day) {
28 var currMonth, monthOffset = offset && !isNaN(offset) ? offset * 3 : 0
29 date = toStringDate(date)
30 if (isValidDate(date)) {
31 currMonth = (getQuarterNumber(date) - 1) * 3
32 date.setMonth(currMonth)
33 return getWhatMonth(date, monthOffset, day)
34 }
35 return date
36}
37
38module.exports = getWhatQuarter