wangrong
2026-08-30 761e5c5fa871959f60e15c971c8d83f2b53d0af5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import dayjs from 'dayjs'
import type { ManipulateType } from 'dayjs'
function getThisStart(val = 'month' as ManipulateType | 'quarter') {
  return new Date(dayjs().startOf(val).format('YYYY/MM/DD HH:mm:ss'))
}
 
function getThisEnd(val = 'month' as ManipulateType | 'quarter') {
  return new Date(dayjs().endOf(val).format('YYYY/MM/DD HH:mm:ss'))
}
 
function getLastStart(val = 'month' as ManipulateType) {
  return new Date(dayjs().subtract(1, val).startOf(val).format('YYYY/MM/DD HH:mm:ss'))
}
 
function getLastEnd(val = 'month' as ManipulateType) {
  return new Date(dayjs().subtract(1, val).endOf(val).format('YYYY/MM/DD HH:mm:ss'))
}
 
function getAround(val = 'month' as ManipulateType, type = 'add', num = 0) {
  if (val === 'week') {
    return new Date(dayjs().endOf('week').add(1, 'day').endOf('day').format('YYYY/MM/DD HH:mm:ss'))
  }
  return new Date(dayjs()[type](num, val).endOf('day').format('YYYY/MM/DD HH:mm:ss'))
}
 
function getAroundStart(val = 'month' as ManipulateType, type = 'add', num = 0) {
  if (val === 'week') {
    return new Date(
      dayjs().startOf('week').add(1, 'day').startOf('day').format('YYYY/MM/DD HH:mm:ss')
    )
  }
  return new Date(dayjs()[type](num, val).startOf('day').format('YYYY/MM/DD HH:mm:ss'))
}
 
function getThisWeek(): [Date, Date] {
  return [
    new Date(dayjs().startOf('week').add(1, 'day').startOf('day').format('YYYY/MM/DD HH:mm:ss')),
    new Date(dayjs().endOf('week').add(1, 'day').endOf('day').format('YYYY/MM/DD HH:mm:ss'))
  ]
}
 
function getCustomRange(relativeToCurrentRange: string): [Date, Date] {
  switch (relativeToCurrentRange) {
    case 'thisYear':
      return [getThisStart('year'), getThisEnd('year')]
    case 'lastYear':
      return [getLastStart('year'), getLastEnd('year')]
    case 'thisMonth':
      return [getThisStart('month'), getThisEnd('month')]
    case 'lastMonth':
      return [getLastStart('month'), getLastEnd('month')]
    case 'thisQuarter':
      return [getThisStart('quarter'), getThisEnd('quarter')]
    case 'thisWeek':
      return getThisWeek()
    case 'LastThreeMonths':
      return [
        new Date(dayjs().subtract(2, 'month').startOf('month').format('YYYY/MM/DD HH:mm:ss')),
        getThisEnd('day')
      ]
    case 'LastSixMonths':
      return [
        new Date(dayjs().subtract(5, 'month').startOf('month').format('YYYY/MM/DD HH:mm:ss')),
        getThisEnd('day')
      ]
    case 'LastTwelveMonths':
      return [
        new Date(dayjs().subtract(11, 'month').startOf('month').format('YYYY/MM/DD HH:mm:ss')),
        getThisEnd('day')
      ]
    case 'YearToThisMonth':
      return [new Date(dayjs().startOf('year').format('YYYY/MM/DD HH:mm:ss')), getThisEnd('month')]
    case 'monthToYesterday':
      return [new Date(dayjs().startOf('month').format('YYYY/MM/DD HH:mm:ss')), getLastEnd('day')]
    case 'today':
      return [getThisStart('day'), getThisEnd('day')]
    case 'yesterday':
      return [getLastStart('day'), getLastEnd('day')]
    case 'LastThreeDays':
      return [
        new Date(dayjs().subtract(2, 'day').startOf('day').format('YYYY/MM/DD HH:mm:ss')),
        getThisEnd('day')
      ]
    case 'monthBeginning':
      return [getThisStart('month'), getThisEnd('day')]
    case 'yearBeginning':
      return [getThisStart('year'), getThisEnd('day')]
    default:
      return [new Date(), new Date()]
  }
}
export {
  getThisStart,
  getThisEnd,
  getLastStart,
  getLastEnd,
  getAround,
  getCustomRange,
  getAroundStart
}