Skip to content

Commit

Permalink
add getWhatQuarter
Browse files Browse the repository at this point in the history
  • Loading branch information
xuliangzhan committed Apr 24, 2021
1 parent 3ab5fc3 commit 49c0abd
Show file tree
Hide file tree
Showing 15 changed files with 174 additions and 24 deletions.
1 change: 1 addition & 0 deletions func/date.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './getWhatYear'
export * from './getWhatQuarter'
export * from './getWhatMonth'
export * from './getWhatDay'
export * from './toStringDate'
Expand Down
2 changes: 2 additions & 0 deletions func/date.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

var getWhatYear = require('./getWhatYear')
var getWhatQuarter = require('./getWhatQuarter')
var getWhatMonth = require('./getWhatMonth')
var getWhatDay = require('./getWhatDay')
var toStringDate = require('./toStringDate')
Expand All @@ -25,6 +26,7 @@ var dateExports = {
toStringDate: toStringDate,
toDateString: toDateString,
getWhatYear: getWhatYear,
getWhatQuarter: getWhatQuarter,
getWhatMonth: getWhatMonth,
getWhatWeek: getWhatWeek,
getWhatDay: getWhatDay,
Expand Down
8 changes: 4 additions & 4 deletions func/getWhatDay.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ var isValidDate = require('./isValidDate')
* 返回前几天或后几天的日期
*
* @param {Date} date 日期或数字
* @param {Number} day 天(默认当天)、前几天、后几天
* @param {Number} offset 天(默认当天)、前几天、后几天
* @param {String} mode 获取时分秒(null默认当前时分秒)、日初(first)、日末(last)
* @return {Date}
*/
function getWhatDay (date, day, mode) {
function getWhatDay (date, offset, mode) {
date = toStringDate(date)
if (isValidDate(date) && !isNaN(day)) {
date.setDate(date.getDate() + staticParseInt(day))
if (isValidDate(date) && !isNaN(offset)) {
date.setDate(date.getDate() + staticParseInt(offset))
if (mode === staticStrFirst) {
return new Date(helperGetDateFullYear(date), helperGetDateMonth(date), date.getDate())
} else if (mode === staticStrLast) {
Expand Down
6 changes: 3 additions & 3 deletions func/getWhatMonth.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* 返回前几月或后几月的日期,可以指定月初(first)、月末(last)、天数,默认当前
* 返回前几月或后几月的日期
* @param date 字符串/日期/时间戳
* @param offset 月偏移量(默认0)、前几个月、后几个月
*/
export declare function getWhatMonth(date: string | Date | number, offset: number): Date;

/**
* 返回前几月或后几月的日期,可以指定月初(first)、月末(last)、天数,默认当前
* 返回前几月或后几月的日期可以指定月初(first)、月末(last)、天数,默认当前
* @param date 字符串/日期/时间戳
* @param offset 月偏移量(默认0)、前几个月、后几个月
* @param offset 月偏移量(默认默认当前月)、前几个月、后几个月
* @param day 获取哪天:月初(first)、月末(last)、指定天数(数值)
*/
export declare function getWhatMonth(date: string | Date | number, offset: number, day: number | 'first' | 'last'): Date;
Expand Down
6 changes: 3 additions & 3 deletions func/getWhatMonth.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ var isNumber = require('./isNumber')
* 返回前几月或后几月的日期
*
* @param {Date} date 日期或数字
* @param {Number} month 月(默认当前月)、前几个月、后几个月
* @param {Number} offset 月(默认当前月)、前几个月、后几个月
* @param {Number/String} day 获取哪天:月初(first)、月末(last)、指定天数(数值),如果为空,但超过指定月份的天数时,则默认单月最后一天
* @return {Date}
*/
function getWhatMonth (date, month, day) {
var monthOffset = month && !isNaN(month) ? month : 0
function getWhatMonth (date, offset, day) {
var monthOffset = offset && !isNaN(offset) ? offset : 0
date = toStringDate(date)
if (isValidDate(date)) {
if (day === staticStrFirst) {
Expand Down
22 changes: 22 additions & 0 deletions func/getWhatQuarter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* 返回前几季度或后几季度的日期
* @param date 字符串/日期/时间戳
* @param offset 季度偏移量(默认0)、前几个季度、后几个季度
*/
export declare function getWhatQuarter(date: string | Date | number, offset: number): Date;

/**
* 返回前几季度或后几季度的日期,可以指定月初(first)、月末(last)、天数,默认当前
* @param date 字符串/日期/时间戳
* @param offset 季度偏移量(默认当前季度)、前几个季度、后几个季度
* @param day 获取哪天:月初(first)、月末(last)、指定天数(数值)
*/
export declare function getWhatQuarter(date: string | Date | number, offset: number, day: number | 'first' | 'last'): Date;

declare module './ctor' {
interface XEUtilsMethods {
getWhatQuarter: typeof getWhatQuarter;
}
}

export default getWhatQuarter
38 changes: 38 additions & 0 deletions func/getWhatQuarter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var getWhatMonth = require('./getWhatMonth')

var toStringDate = require('./toStringDate')

var isValidDate = require('./isValidDate')

function getQuarterNumber (date) {
var month = date.getMonth()
if (month < 3) {
return 1
} else if (month < 6) {
return 2
} else if (month < 9) {
return 3
}
return 4
}

/**
* 返回前几季度或后几季度的日期
*
* @param {Date} date 日期
* @param {Number} offset 季度(默认当前季度)、前几季度、后几季度
* @param {Number} day 获取哪天:月初(first)、月末(last)、指定天数(数值),如果为空,但超过指定月份的天数时,则默认单月最后一天
* @return {Date}
*/
function getWhatQuarter (date, offset, day) {
var currMonth, monthOffset = offset && !isNaN(offset) ? offset * 3 : 0
date = toStringDate(date)
if (isValidDate(date)) {
currMonth = (getQuarterNumber(date) - 1) * 3
date.setMonth(currMonth)
return getWhatMonth(date, monthOffset, day)
}
return date
}

module.exports = getWhatQuarter
6 changes: 3 additions & 3 deletions func/getWhatWeek.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* 返回前几周或后几周的日期,可以指定星期几(0~6),默认当前
* 返回前几周或后几周的日期
* @param date 字符串/日期/时间戳
* @param offset 周偏移量(默认0)、前几周、后几周
* @param offset 周偏移量(默认当前周)、前几周、后几周
*/
export declare function getWhatWeek(date: string | Date | number, offset: number): Date;

/**
* 返回前几周或后几周的日期,可以指定星期几(0~6),默认当前
* @param date 字符串/日期/时间戳
* @param offset 周偏移量(默认0)、前几周、后几周
* @param offset 周偏移量(默认当前周)、前几周、后几周
* @param day 星期天(0)、星期一(1)、星期二(2)、星期三(3)、星期四(4)、星期五(5)、星期六(6)
*/
export declare function getWhatWeek(date: string | Date | number, offset: number, day: number): Date;
Expand Down
8 changes: 4 additions & 4 deletions func/getWhatWeek.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ var isValidDate = require('./isValidDate')
* 返回前几周或后几周的星期几
*
* @param {Date} date 日期
* @param {Number} week 周(默认当前周)、前几周、后几周
* @param {Number} offset 周(默认当前周)、前几周、后几周
* @param {Number} day 星期天(默认0)、星期一(1)、星期二(2)、星期三(3)、星期四(4)、星期五(5)、星期六(6)
* @return {Date}
*/
function getWhatWeek (date, week, day) {
function getWhatWeek (date, offset, day) {
var time, whatDayTime, currentDay, customDay
date = toStringDate(date)
if (isValidDate(date)) {
customDay = staticParseInt(/^[0-7]$/.test(day) ? day : date.getDay())
currentDay = date.getDay()
time = helperGetDateTime(date)
whatDayTime = time + ((customDay === 0 ? 7 : customDay) - (currentDay === 0 ? 7 : currentDay)) * staticDayTime
if (week && !isNaN(week)) {
whatDayTime += week * staticWeekTime
if (offset && !isNaN(offset)) {
whatDayTime += offset * staticWeekTime
}
return new Date(whatDayTime)
}
Expand Down
4 changes: 2 additions & 2 deletions func/getWhatYear.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* 返回前几年或后几年的日期,可以指定年初(first)、年末(last)、月份(0~11),默认当前
* 返回前几年或后几年的日期
* @param date 字符串/日期/时间戳
* @param year 年(默认当前年)、前几个年(数值)、后几个年(数值)
*/
export declare function getWhatYear(date: string | Date | number, offset: number): Date;

/**
* 返回前几年或后几年的日期,可以指定年初(first)、年末(last)、月份(0~11),默认当前
* 返回前几年或后几年的日期可以指定年初(first)、年末(last)、月份(0~11),默认当前
* @param date 字符串/日期/时间戳
* @param year 年(默认当前年)、前几个年(数值)、后几个年(数值)
* @param month 获取哪月:年初(first)、年末(last)、指定月份(0-11)
Expand Down
8 changes: 4 additions & 4 deletions func/getWhatYear.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ var isValidDate = require('./isValidDate')
* 返回前几年或后几年的日期
*
* @param {Date} date 日期或数字
* @param {Number} year 年(默认当前年)、前几个年(数值)、后几个年(数值)
* @param {Number} offset 年(默认当前年)、前几个年(数值)、后几个年(数值)
* @param {Number/String} month 获取哪月(null默认当前年)、年初(first)、年末(last)、指定月份(0-11)
* @return {Date}
*/
function getWhatYear (date, year, month) {
function getWhatYear (date, offset, month) {
var number
date = toStringDate(date)
if (isValidDate(date)) {
if (year) {
number = year && !isNaN(year) ? year : 0
if (offset) {
number = offset && !isNaN(offset) ? offset : 0
date.setFullYear(helperGetDateFullYear(date) + number)
}
if (month || !isNaN(month)) {
Expand Down
1 change: 1 addition & 0 deletions func/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export * from './mean'

// date
export * from './getWhatYear'
export * from './getWhatQuarter'
export * from './getWhatMonth'
export * from './getWhatDay'
export * from './toStringDate'
Expand Down
2 changes: 2 additions & 0 deletions func/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ var mean = require('./mean')

// 日期相关的方法
var getWhatYear = require('./getWhatYear')
var getWhatQuarter = require('./getWhatQuarter')
var getWhatMonth = require('./getWhatMonth')
var getWhatDay = require('./getWhatDay')
var toStringDate = require('./toStringDate')
Expand Down Expand Up @@ -331,6 +332,7 @@ assign(XEUtils, {
toStringDate: toStringDate,
toDateString: toDateString,
getWhatYear: getWhatYear,
getWhatQuarter: getWhatQuarter,
getWhatMonth: getWhatMonth,
getWhatWeek: getWhatWeek,
getWhatDay: getWhatDay,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xe-utils",
"version": "3.2.0",
"version": "3.2.1",
"description": "JavaScript 函数库、工具类",
"main": "index.js",
"unpkg": "dist/xe-utils.umd.min.js",
Expand Down
84 changes: 84 additions & 0 deletions test/date.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,90 @@ describe('Date functions', () => {
).toEqual(new Date(2017, 11, 31, 23, 59, 59, 999))
})

test('getWhatQuarter()', () => {
expect(
XEUtils.getWhatQuarter().toString()
).toEqual('Invalid Date')
expect(
XEUtils.getWhatQuarter(0).toString()
).toEqual('Invalid Date')
expect(
XEUtils.getWhatQuarter(-1).toString()
).toEqual('Invalid Date')
expect(
XEUtils.getWhatQuarter({}).toString()
).toEqual('Invalid Date')
expect(
XEUtils.getWhatQuarter([]).toString()
).toEqual('Invalid Date')
expect(
XEUtils.getWhatQuarter(null).toString()
).toEqual('Invalid Date')
expect(
XEUtils.getWhatQuarter(undefined).toString()
).toEqual('Invalid Date')
expect(
XEUtils.getWhatQuarter([2018, 1, 1]).toString()
).toEqual('Invalid Date')
expect(
XEUtils.getWhatQuarter({ time: 2018 }).toString()
).toEqual('Invalid Date')
expect(
XEUtils.getWhatQuarter('null').toString()
).toEqual('Invalid Date')
expect(
XEUtils.getWhatQuarter(new Date('')).toString()
).toEqual('Invalid Date')
expect(
XEUtils.getWhatQuarter(new Date('abc')).toString()
).toEqual('Invalid Date')
expect(
XEUtils.getWhatQuarter(date, -1)
).toEqual(new Date(2016, 9, 1, 14, 5, 30, 99))
expect(
XEUtils.getWhatQuarter(time, 1)
).toEqual(new Date(2017, 3, 1, 14, 5, 30, 99))
expect(
XEUtils.getWhatQuarter('2017-12-20', -1)
).toEqual(new Date(2017, 6, 20))
expect(
XEUtils.getWhatQuarter('2017-12-20', 1)
).toEqual(new Date(2018, 0, 20))
expect(
XEUtils.getWhatQuarter(new Date(2017, 11, 20), 1)
).toEqual(new Date(2018, 0, 20))
expect(
XEUtils.getWhatQuarter(new Date(2017, 11, 20), 3)
).toEqual(new Date(2018, 6, 20))
expect(
XEUtils.getWhatQuarter('2017-12-20', -1, 'first')
).toEqual(new Date(2017, 6, 1, 0, 0, 0, 0))
expect(
XEUtils.getWhatQuarter('2017-12-20', 1, 'last')
).toEqual(new Date(2018, 0, 31, 23, 59, 59, 999))
expect(
XEUtils.getWhatQuarter('2021-01-31', -1)
).toEqual(new Date(2020, 9, 31))
expect(
XEUtils.getWhatQuarter('2021-01-31', -3)
).toEqual(new Date(2020, 3, 30))
expect(
XEUtils.getWhatQuarter('2021-01-31', 1)
).toEqual(new Date(2021, 3, 30))
expect(
XEUtils.getWhatQuarter('2021-01-31', 1, 'last')
).toEqual(new Date(2021, 3, 30, 23, 59, 59, 999))
expect(
XEUtils.getWhatQuarter('2021-01-31', 6)
).toEqual(new Date(2022, 6, 31))
expect(
XEUtils.getWhatQuarter('2021-01-31', -6)
).toEqual(new Date(2019, 6, 31))
expect(
XEUtils.getWhatQuarter('2021-01-31', 12)
).toEqual(new Date(2024, 0, 31))
})

test('getWhatMonth()', () => {
expect(
XEUtils.getWhatMonth().toString()
Expand Down

0 comments on commit 49c0abd

Please sign in to comment.