zouyu
2026-04-20 df3902e7eae5dd38a2d47aa63e5f27131f701db9
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import dayjs from 'dayjs';
/**
 * 获取年月日
 */
export function getYearAndMonthAndDays(date=new Date()) {
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  if (month < 10) {
    month = '0' + month + '-'
  } else {
    month = month + '-'
  }
  year = year + '-'
  let days = date.getDate()
  if (days < 10) {
    days = '0' + days
  } else {
    days = days
  }
  return (year + month + days)
}
/**
 * 日期格式化
 */
export function dateFormat(date, format = 'yyyy-MM-dd hh:mm:ss') {
  if (date !== 'Invalid Date') {
    var o = {
      'M+': date.getMonth() + 1, // month
      'd+': date.getDate(), // day
      'h+': date.getHours(), // hour
      'm+': date.getMinutes(), // minute
      's+': date.getSeconds(), // second
      'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
      S: date.getMilliseconds() // millisecond
    }
    if (/(y+)/.test(format)) {
      format = format.replace(
        RegExp.$1,
        (date.getFullYear() + '').substr(4 - RegExp.$1.length)
      )
    }
    for (var k in o) {
      if (new RegExp('(' + k + ')').test(format)) {
        format = format.replace(
          RegExp.$1,
          RegExp.$1.length === 1
            ? o[k]
            : ('00' + o[k]).substr(('' + o[k]).length)
        )
      }
    }
    return format
  }
  return ''
}
 
/**
 * 获取年月日  26号+1
 * @param date
 * @returns {string}
 */
export function getYearAndMonthAndDaysZTNS(date = new Date()) {
  let year = date.getFullYear();
  let month = date.getMonth();
  let days = date.getDate();
 
  // 判断是否是26号
  if (days === 26) {
    month++;
    if (month > 11) {
      month = 0;
      year++;
    }
  }
  month += 1;
  month = month < 10 ? '0' + month + '-' : month + '-';
  year = year + '-';
  days = days < 10 ? '0' + days : days;
  return (year + month + days);
}
 
/**
 * 获取时间范围:26号-25号
 * 初始化默认日期范围:近一个月(当前日期 - 30天 至 当前日期)
 * @param format
 * @returns {*[]}
 */
export function getTimeRange(format = 'YYYY-MM-DD HH:mm:ss') {
  // 获取当前时间
  const now = dayjs();
  // 获取当前日期的「日」(1-31)
  const currentDate = now.date();
 
  let startTime, endTime;
 
  // 核心逻辑:判断当前日期是否大于25号
  if (currentDate > 25) {
    // ✅ 情况1:当前日>25 → 当月26号 ~ 次月25号
    startTime = now.startOf('month').add(25, 'day'); // 当月1号 +25天 = 26号
    endTime = startTime.add(1, 'month').date(25).hour(23)
      .minute(59)
      .second(59);     // 次月25号(dayjs自动处理跨年)
  } else {
    // ✅ 情况2:当前日≤25 → 上月26号 ~ 当月25号
    startTime = now.subtract(1, 'month').startOf('month').add(25, 'day'); // 上月26号
    endTime = now.date(25).hour(23)
      .minute(59)
      .second(59); // 当月25号
  }
 
  // 返回格式化后的时间数组
  return [startTime.format(format), endTime.format(format)];
}
 
/**
 * 获取班次月份
 * @returns {*}
 */
export function getWorkMonth(){
  // 获取当前时间
  const now = dayjs();
  // 获取当前日期的「日」(1-31)
  const currentDate = now.date();
  let workMonth;
  // 核心逻辑:判断当前日期是否大于25号
  if (currentDate > 25) {
    workMonth = now.add(1, 'month')
  }else{
    workMonth = now
  }
  return workMonth
}
 
/**
 * 判断时间区间是否超过 1 个自然月
 * @param {string} startTime - 开始时间 yyyy-MM-dd HH:mm:ss
 * @param {string} endTime - 结束时间 yyyy-MM-dd HH:mm:ss
 * @returns {boolean} true=超过一个月,false=未超过
 */
export function isOverOneMonth(startTime, endTime) {
  // 1. 解析为日期对象
  const start = new Date(startTime);
  const end = new Date(endTime);
 
  // 2. 校验日期合法性
  if (isNaN(start.getTime()) || isNaN(end.getTime())) {
    throw new Error("时间格式错误,请使用 yyyy-MM-dd HH:mm:ss 格式");
  }
 
  // 3. 如果结束时间早于开始时间,直接返回false
  if (end < start) return false;
 
  // 4. 计算年份差、月份差
  const startYear = start.getFullYear();
  const startMonth = start.getMonth();
  const startDay = start.getDate();
  const startRest = start.getTime() - new Date(startYear, startMonth, startDay).getTime(); // 时分秒毫秒
 
  const endYear = end.getFullYear();
  const endMonth = end.getMonth();
  const endDay = end.getDate();
  const endRest = end.getTime() - new Date(endYear, endMonth, endDay).getTime();
 
  // 总月份差值
  const monthDiff = (endYear - startYear) * 12 + (endMonth - startMonth);
 
  // 5. 判断逻辑
  if (monthDiff > 1) {
    return true; // 月份差>1 → 超过
  } else if (monthDiff === 1) {
    // 月份差=1 → 比较 日+时分秒,超过则判定超时
    return endDay > startDay || (endDay === startDay && endRest > startRest);
  } else {
    return false; // 月份差<1 → 未超过
  }
}