schangxiang@126.com
2025-09-10 3d43ffa3152110b7823f9fa6320c08a6ae02358a
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
177
178
179
180
/**
 * 时间日期转换
 * @param date 当前时间,new Date() 格式
 * @param format 需要转换的时间格式字符串
 * @description format 字符串随意,如 `YYYY-mm、YYYY-mm-dd`
 * @description format 季度:"YYYY-mm-dd HH:MM:SS QQQQ"
 * @description format 星期:"YYYY-mm-dd HH:MM:SS WWW"
 * @description format 几周:"YYYY-mm-dd HH:MM:SS ZZZ"
 * @description format 季度 + 星期 + 几周:"YYYY-mm-dd HH:MM:SS WWW QQQQ ZZZ"
 * @returns 返回拼接后的时间字符串
 */
export function formatDate(date, format) {
    const we = date.getDay(); // 星期
    const z = getWeek(date); // 周
    const qut = Math.floor((date.getMonth() + 3) / 3).toString(); // 季度
    const opt = {
        'Y+': date.getFullYear().toString(), // 年
        'm+': (date.getMonth() + 1).toString(), // 月(月份从0开始,要+1)
        'd+': date.getDate().toString(), // 日
        'H+': date.getHours().toString(), // 时
        'M+': date.getMinutes().toString(), // 分
        'S+': date.getSeconds().toString(), // 秒
        'q+': qut, // 季度
    };
    // 中文数字 (星期)
    const week = {
        '0': '日',
        '1': '一',
        '2': '二',
        '3': '三',
        '4': '四',
        '5': '五',
        '6': '六',
    };
    // 中文数字(季度)
    const quarter = {
        '1': '一',
        '2': '二',
        '3': '三',
        '4': '四',
    };
    if (/(W+)/.test(format))
        format = format.replace(RegExp.$1, RegExp.$1.length > 1 ? (RegExp.$1.length > 2 ? '星期' + week[we] : '周' + week[we]) : week[we]);
    if (/(Q+)/.test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 4 ? '第' + quarter[qut] + '季度' : quarter[qut]);
    if (/(Z+)/.test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 3 ? '第' + z + '周' : z + '');
    for (const k in opt) {
        const r = new RegExp('(' + k + ')').exec(format);
        // 若输入的长度不为1,则前面补零
        if (r) format = format.replace(r[1], RegExp.$1.length == 1 ? opt[k] : opt[k].padStart(RegExp.$1.length, '0'));
    }
    return format;
}
 
/**
 * 时间日期转换
 * @param date 当前时间,new Date() 格式
 * @param format 需要转换的时间格式字符串
 * @description format 字符串随意,如 `YYYY-mm、YYYY-mm-dd`
 * @description format 季度:"YYYY-mm-dd HH:MM:SS QQQQ"
 * @description format 星期:"YYYY-mm-dd HH:MM:SS WWW"
 * @description format 几周:"YYYY-mm-dd HH:MM:SS ZZZ"
 * @description format 季度 + 星期 + 几周:"YYYY/MM/DD HH:mm:ss WWW QQQQ ZZZ"
 * @returns 返回拼接后的时间字符串   YYYY-MM-DD HH:mm:ss
 */
export function formatDateV2(date, format) {
    //debugger
    const we = date.getDay(); // 星期
    const z = getWeek(date); // 周
    const qut = Math.floor((date.getMonth() + 3) / 3).toString(); // 季度
    const opt = {
        'Y+': date.getFullYear().toString(), // 年
        'M+': (date.getMonth() + 1).toString(), // 月(月份从0开始,要+1)
        'D+': date.getDate().toString(), // 日
        'H+': date.getHours().toString(), // 时
        'm+': date.getMinutes().toString(), // 分
        's+': date.getSeconds().toString(), // 秒
        'q+': qut, // 季度
    };
    // 中文数字 (星期)
    const week = {
        '0': '日',
        '1': '一',
        '2': '二',
        '3': '三',
        '4': '四',
        '5': '五',
        '6': '六',
    };
    // 中文数字(季度)
    const quarter = {
        '1': '一',
        '2': '二',
        '3': '三',
        '4': '四',
    };
    if (/(W+)/.test(format))
        format = format.replace(RegExp.$1, RegExp.$1.length > 1 ? (RegExp.$1.length > 2 ? '星期' + week[we] : '周' + week[we]) : week[we]);
    if (/(Q+)/.test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 4 ? '第' + quarter[qut] + '季度' : quarter[qut]);
    if (/(Z+)/.test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 3 ? '第' + z + '周' : z + '');
    for (const k in opt) {
        const r = new RegExp('(' + k + ')').exec(format);
        // 若输入的长度不为1,则前面补零
        if (r) format = format.replace(r[1], RegExp.$1.length == 1 ? opt[k] : opt[k].padStart(RegExp.$1.length, '0'));
    }
    return format;
}
 
/**
 * 获取当前日期是第几周
 * @param dateTime 当前传入的日期值
 * @returns 返回第几周数字值
 */
export function getWeek(dateTime) {
    const temptTime = new Date(dateTime.getTime());
    // 周几
    const weekday = temptTime.getDay() || 7;
    // 周1+5天=周六
    temptTime.setDate(temptTime.getDate() - weekday + 1 + 5);
    let firstDay = new Date(temptTime.getFullYear(), 0, 1);
    const dayOfWeek = firstDay.getDay();
    let spendDay = 1;
    if (dayOfWeek != 0) spendDay = 7 - dayOfWeek + 1;
    firstDay = new Date(temptTime.getFullYear(), 0, 1 + spendDay);
    const d = Math.ceil((temptTime.valueOf() - firstDay.valueOf()) / 86400000);
    const result = Math.ceil(d / 7);
    return result;
}
 
/**
 * 时间问候语
 * @param param 当前时间,new Date() 格式
 * @description param 调用 `formatAxis(new Date())` 输出 `上午好`
 * @returns 返回拼接后的时间字符串
 */
export function formatAxis(param) {
    const hour = new Date(param).getHours();
    if (hour < 6) return '凌晨好';
    else if (hour < 9) return '早上好';
    else if (hour < 12) return '上午好';
    else if (hour < 14) return '中午好';
    else if (hour < 17) return '下午好';
    else if (hour < 19) return '傍晚好';
    else if (hour < 22) return '晚上好';
    else return '夜里好';
}
 
//默认赋值 当前一周的时间
export function getThisWeekRange(flag) {
 
    //暂时都返回空
    //return ["",""];
 
    const today = new Date();
    const startOfWeek = new Date();
    // if(flag==1){//表示要查7天内数据
    //   //startOfWeek.setDate(today.getDate() -6); // 减去今天是一周的第几天,得到周一的日期startOfWeek
    //   startOfWeek.setDate(today.getDate() -2); // 减去今天是一周的第几天,得到周一的日期startOfWeek
    // }
    startOfWeek.setDate(today.getDate() - flag); // 减去今天是一周的第几天,得到周一的日期startOfWeek
    startOfWeek.setHours(0, 0, 0, 0); // 设置时、分、秒和毫秒数
 
    const endOfWeek = new Date(today);
    endOfWeek.setDate(endOfWeek.getDate() + 1);
    //endOfWeek.setHours(23, 59, 59, 999); // 设置时、分、秒和毫秒数
    endOfWeek.setHours(0, 0, 0, 0); // 设置时、分、秒和毫秒数
 
 
    //return [moment(startOfWeek,"YYYY-MM-DD HH:mm"), moment(endOfWeek,"YYYY-MM-DD HH:mm")];
    //return [moment(startOfWeek), moment(endOfWeek)];
 
    // const startStr = formatDateV2(startOfWeek,'YYYY-MM-DD HH:mm:ss');
    // const endStr = formatDateV2(endOfWeek,'YYYY-MM-DD HH:mm:ss');
    //return [new Date(startStr), new Date(endStr)];
    // const startStr = formatDateV2(startOfWeek, 'yyyy-MM-dd HH:mm:ss');
    // const endStr = formatDateV2(endOfWeek, 'yyyy-MM-dd HH:mm:ss');
    // return [startStr, endStr];
     // ✅ 直接返回 Date 对象的数组
     return [startOfWeek, endOfWeek];
    //*/
}