From 79f08c362ca037a94c3759f38be7341f6f5548b0 Mon Sep 17 00:00:00 2001 From: lyt <1105290566@qq.com> Date: Mon, 21 Jun 2021 15:49:16 +0800 Subject: [PATCH] =?UTF-8?q?'admin-21.06.21:=E4=BC=98=E5=8C=96=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E5=B7=A5=E5=85=B7=E7=B1=BB=EF=BC=8C=E6=84=9F=E8=B0=A2?= =?UTF-8?q?=E7=BE=A4=E5=8F=8B@=E6=88=91=E8=A7=89=E5=BE=97=E8=BF=98?= =?UTF-8?q?=E8=A1=8C'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/formatTime.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/utils/formatTime.ts b/src/utils/formatTime.ts index 632d6be7..441e30c3 100644 --- a/src/utils/formatTime.ts +++ b/src/utils/formatTime.ts @@ -5,11 +5,13 @@ * @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 WWW QQQQ" + * @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: Date, format: string): string { let we = date.getDay(); // 星期 + let z = getWeek(date); // 周 let qut = Math.floor((date.getMonth() + 3) / 3).toString(); // 季度 const opt: { [key: string]: string } = { 'Y+': date.getFullYear().toString(), // 年 @@ -40,6 +42,7 @@ export function formatDate(date: Date, format: string): string { 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 (let k in opt) { let r = new RegExp('(' + k + ')').exec(format); // 若输入的长度不为1,则前面补零 @@ -48,6 +51,27 @@ export function formatDate(date: Date, format: string): string { return format; } +/** + * 获取当前日期是第几周 + * @param dateTime 当前传入的日期值 + * @returns 返回第几周数字值 + */ +export function getWeek(dateTime: Date): number { + let temptTime = new Date(dateTime.getTime()); + // 周几 + let weekday = temptTime.getDay() || 7; + // 周1+5天=周六 + temptTime.setDate(temptTime.getDate() - weekday + 1 + 5); + let firstDay = new Date(temptTime.getFullYear(), 0, 1); + let dayOfWeek = firstDay.getDay(); + let spendDay = 1; + if (dayOfWeek != 0) spendDay = 7 - dayOfWeek + 1; + firstDay = new Date(temptTime.getFullYear(), 0, 1 + spendDay); + let d = Math.ceil((temptTime.valueOf() - firstDay.valueOf()) / 86400000); + let result = Math.ceil(d / 7); + return result; +} + /** * 将时间转换为 `几秒前`、`几分钟前`、`几小时前`、`几天前` * @param param 当前时间,new Date() 格式或者字符串时间格式