From a1d7e81859f554f3a53680cc35f0f49bf1f77098 Mon Sep 17 00:00:00 2001
From: wwf <1971391498@qq.com>
Date: 星期四, 14 五月 2026 14:37:02 +0800
Subject: [PATCH] 导入项目
---
src/utils/formatTime.ts | 332 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 332 insertions(+), 0 deletions(-)
diff --git a/src/utils/formatTime.ts b/src/utils/formatTime.ts
new file mode 100644
index 0000000..99eb428
--- /dev/null
+++ b/src/utils/formatTime.ts
@@ -0,0 +1,332 @@
+import dayjs from 'dayjs'
+import type { TableColumnCtx } from 'element-plus'
+
+/**
+ * 鏃ユ湡蹇嵎閫夐」閫傜敤浜� el-date-picker
+ */
+export const defaultShortcuts = [
+ {
+ text: '浠婂ぉ',
+ value: () => {
+ return new Date()
+ }
+ },
+ {
+ text: '鏄ㄥぉ',
+ value: () => {
+ const date = new Date()
+ date.setTime(date.getTime() - 3600 * 1000 * 24)
+ return [date, date]
+ }
+ },
+ {
+ text: '鏈�杩戜竷澶�',
+ value: () => {
+ const date = new Date()
+ date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
+ return [date, new Date()]
+ }
+ },
+ {
+ text: '鏈�杩� 30 澶�',
+ value: () => {
+ const date = new Date()
+ date.setTime(date.getTime() - 3600 * 1000 * 24 * 30)
+ return [date, new Date()]
+ }
+ },
+ {
+ text: '鏈湀',
+ value: () => {
+ const date = new Date()
+ date.setDate(1) // 璁剧疆涓哄綋鍓嶆湀鐨勭涓�澶�
+ return [date, new Date()]
+ }
+ },
+ {
+ text: '浠婂勾',
+ value: () => {
+ const date = new Date()
+ return [new Date(`${date.getFullYear()}-01-01`), date]
+ }
+ }
+]
+
+/**
+ * 鏃堕棿鏃ユ湡杞崲
+ * @param date 褰撳墠鏃堕棿锛宯ew Date() 鏍煎紡
+ * @param format 闇�瑕佽浆鎹㈢殑鏃堕棿鏍煎紡瀛楃涓�
+ * @description format 瀛楃涓查殢鎰忥紝濡� `YYYY-MM銆乊YYY-MM-DD`
+ * @description format 瀛e害锛�"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 瀛e害 + 鏄熸湡 + 鍑犲懆锛�"YYYY-MM-DD HH:mm:ss WWW QQQQ ZZZ"
+ * @returns 杩斿洖鎷兼帴鍚庣殑鏃堕棿瀛楃涓�
+ */
+export function formatDate(date: Date, format?: string): string {
+ // 鏃ユ湡涓嶅瓨鍦紝鍒欒繑鍥炵┖
+ if (!date) {
+ return ''
+ }
+ // 鏃ユ湡瀛樺湪锛屽垯杩涜鏍煎紡鍖�
+ return date ? dayjs(date).format(format ?? 'YYYY-MM-DD HH:mm:ss') : ''
+}
+
+/**
+ * 鑾峰彇褰撳墠鐨勬棩鏈�+鏃堕棿
+ */
+export function getNowDateTime() {
+ return dayjs()
+}
+
+/**
+ * 鑾峰彇褰撳墠鏃ユ湡鏄鍑犲懆
+ * @param dateTime 褰撳墠浼犲叆鐨勬棩鏈熷��
+ * @returns 杩斿洖绗嚑鍛ㄦ暟瀛楀��
+ */
+export function getWeek(dateTime: Date): number {
+ 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)
+ return Math.ceil(d / 7)
+}
+
+/**
+ * 灏嗘椂闂磋浆鎹负 `鍑犵鍓峘銆乣鍑犲垎閽熷墠`銆乣鍑犲皬鏃跺墠`銆乣鍑犲ぉ鍓峘
+ * @param param 褰撳墠鏃堕棿锛宯ew Date() 鏍煎紡鎴栬�呭瓧绗︿覆鏃堕棿鏍煎紡
+ * @param format 闇�瑕佽浆鎹㈢殑鏃堕棿鏍煎紡瀛楃涓�
+ * @description param 10绉掞細 10 * 1000
+ * @description param 1鍒嗭細 60 * 1000
+ * @description param 1灏忔椂锛� 60 * 60 * 1000
+ * @description param 24灏忔椂锛�60 * 60 * 24 * 1000
+ * @description param 3澶╋細 60 * 60* 24 * 1000 * 3
+ * @returns 杩斿洖鎷兼帴鍚庣殑鏃堕棿瀛楃涓�
+ */
+export function formatPast(param: string | Date, format = 'YYYY-MM-DD HH:mm:ss'): string {
+ // 浼犲叆鏍煎紡澶勭悊銆佸瓨鍌ㄨ浆鎹㈠��
+ let t: any, s: number
+ // 鑾峰彇js 鏃堕棿鎴�
+ let time: number = new Date().getTime()
+ // 鏄惁鏄璞�
+ typeof param === 'string' || 'object' ? (t = new Date(param).getTime()) : (t = param)
+ // 褰撳墠鏃堕棿鎴� - 浼犲叆鏃堕棿鎴�
+ time = Number.parseInt(`${time - t}`)
+ if (time < 10000) {
+ // 10绉掑唴
+ return '鍒氬垰'
+ } else if (time < 60000 && time >= 10000) {
+ // 瓒呰繃10绉掑皯浜�1鍒嗛挓鍐�
+ s = Math.floor(time / 1000)
+ return `${s}绉掑墠`
+ } else if (time < 3600000 && time >= 60000) {
+ // 瓒呰繃1鍒嗛挓灏戜簬1灏忔椂
+ s = Math.floor(time / 60000)
+ return `${s}鍒嗛挓鍓峘
+ } else if (time < 86400000 && time >= 3600000) {
+ // 瓒呰繃1灏忔椂灏戜簬24灏忔椂
+ s = Math.floor(time / 3600000)
+ return `${s}灏忔椂鍓峘
+ } else if (time < 259200000 && time >= 86400000) {
+ // 瓒呰繃1澶╁皯浜�3澶╁唴
+ s = Math.floor(time / 86400000)
+ return `${s}澶╁墠`
+ } else {
+ // 瓒呰繃3澶�
+ const date = typeof param === 'string' || 'object' ? new Date(param) : param
+ return formatDate(date, format)
+ }
+}
+
+/**
+ * 鏃堕棿闂�欒
+ * @param param 褰撳墠鏃堕棿锛宯ew Date() 鏍煎紡
+ * @description param 璋冪敤 `formatAxis(new Date())` 杈撳嚭 `涓婂崍濂絗
+ * @returns 杩斿洖鎷兼帴鍚庣殑鏃堕棿瀛楃涓�
+ */
+export function formatAxis(param: Date): string {
+ const hour: number = 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 '澶滈噷濂�'
+}
+
+/**
+ * 灏嗘绉掞紝杞崲鎴愭椂闂村瓧绗︿覆銆備緥濡傝锛寈x 鍒嗛挓
+ *
+ * @param ms 姣
+ * @returns {string} 瀛楃涓�
+ */
+export function formatPast2(ms: number): string {
+ const day = Math.floor(ms / (24 * 60 * 60 * 1000))
+ const hour = Math.floor(ms / (60 * 60 * 1000) - day * 24)
+ const minute = Math.floor(ms / (60 * 1000) - day * 24 * 60 - hour * 60)
+ const second = Math.floor(ms / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60)
+ if (day > 0) {
+ return day + ' 澶�' + hour + ' 灏忔椂 ' + minute + ' 鍒嗛挓'
+ }
+ if (hour > 0) {
+ return hour + ' 灏忔椂 ' + minute + ' 鍒嗛挓'
+ }
+ if (minute > 0) {
+ return minute + ' 鍒嗛挓'
+ }
+ if (second > 0) {
+ return second + ' 绉�'
+ } else {
+ return 0 + ' 绉�'
+ }
+}
+
+/**
+ * element plus 鐨勬椂闂� Formatter 瀹炵幇锛屼娇鐢� YYYY-MM-DD HH:mm:ss 鏍煎紡
+ *
+ * @param row 琛屾暟鎹�
+ * @param column 瀛楁
+ * @param cellValue 瀛楁鍊�
+ */
+export function dateFormatter(_row: any, _column: TableColumnCtx<any>, cellValue: any): string {
+ return cellValue ? formatDate(cellValue) : ''
+}
+
+/**
+ * element plus 鐨勬椂闂� Formatter 瀹炵幇锛屼娇鐢� YYYY-MM-DD 鏍煎紡
+ *
+ * @param row 琛屾暟鎹�
+ * @param column 瀛楁
+ * @param cellValue 瀛楁鍊�
+ */
+export function dateFormatter2(_row: any, _column: TableColumnCtx<any>, cellValue: any): string {
+ return cellValue ? formatDate(cellValue, 'YYYY-MM-DD') : ''
+}
+
+/**
+ * 璁剧疆璧峰鏃ユ湡锛屾椂闂翠负00:00:00
+ * @param param 浼犲叆鏃ユ湡
+ * @returns 甯︽椂闂�00:00:00鐨勬棩鏈�
+ */
+export function beginOfDay(param: Date): Date {
+ return new Date(param.getFullYear(), param.getMonth(), param.getDate(), 0, 0, 0)
+}
+
+/**
+ * 璁剧疆缁撴潫鏃ユ湡锛屾椂闂翠负23:59:59
+ * @param param 浼犲叆鏃ユ湡
+ * @returns 甯︽椂闂�23:59:59鐨勬棩鏈�
+ */
+export function endOfDay(param: Date): Date {
+ return new Date(param.getFullYear(), param.getMonth(), param.getDate(), 23, 59, 59)
+}
+
+/**
+ * 璁$畻涓や釜鏃ユ湡闂撮殧澶╂暟
+ * @param param1 鏃ユ湡1
+ * @param param2 鏃ユ湡2
+ */
+export function betweenDay(param1: Date, param2: Date): number {
+ param1 = convertDate(param1)
+ param2 = convertDate(param2)
+ // 璁$畻宸��
+ return Math.floor((param2.getTime() - param1.getTime()) / (24 * 3600 * 1000))
+}
+
+/**
+ * 鏃ユ湡璁$畻
+ * @param param1 鏃ユ湡
+ * @param param2 娣诲姞鐨勬椂闂�
+ */
+export function addTime(param1: Date, param2: number): Date {
+ param1 = convertDate(param1)
+ return new Date(param1.getTime() + param2)
+}
+
+/**
+ * 鏃ユ湡杞崲
+ * @param param 鏃ユ湡
+ */
+export function convertDate(param: Date | string): Date {
+ if (typeof param === 'string') {
+ return new Date(param)
+ }
+ return param
+}
+
+/**
+ * 鎸囧畾鐨勪袱涓棩鏈�, 鏄惁涓哄悓涓�澶�
+ * @param a 鏃ユ湡 A
+ * @param b 鏃ユ湡 B
+ */
+export function isSameDay(a: dayjs.ConfigType, b: dayjs.ConfigType): boolean {
+ if (!a || !b) return false
+
+ const aa = dayjs(a)
+ const bb = dayjs(b)
+ return aa.year() == bb.year() && aa.month() == bb.month() && aa.day() == bb.day()
+}
+
+/**
+ * 鑾峰彇涓�澶╃殑寮�濮嬫椂闂淬�佹埅姝㈡椂闂�
+ * @param date 鏃ユ湡
+ * @param days 澶╂暟
+ */
+export function getDayRange(
+ date: dayjs.ConfigType,
+ days: number
+): [dayjs.ConfigType, dayjs.ConfigType] {
+ const day = dayjs(date).add(days, 'd')
+ return getDateRange(day, day)
+}
+
+/**
+ * 鑾峰彇鏈�杩�7澶╃殑寮�濮嬫椂闂淬�佹埅姝㈡椂闂�
+ */
+export function getLast7Days(): [dayjs.ConfigType, dayjs.ConfigType] {
+ const lastWeekDay = dayjs().subtract(7, 'd')
+ const yesterday = dayjs().subtract(1, 'd')
+ return getDateRange(lastWeekDay, yesterday)
+}
+
+/**
+ * 鑾峰彇鏈�杩�30澶╃殑寮�濮嬫椂闂淬�佹埅姝㈡椂闂�
+ */
+export function getLast30Days(): [dayjs.ConfigType, dayjs.ConfigType] {
+ const lastMonthDay = dayjs().subtract(30, 'd')
+ const yesterday = dayjs().subtract(1, 'd')
+ return getDateRange(lastMonthDay, yesterday)
+}
+
+/**
+ * 鑾峰彇鏈�杩�1骞寸殑寮�濮嬫椂闂淬�佹埅姝㈡椂闂�
+ */
+export function getLast1Year(): [dayjs.ConfigType, dayjs.ConfigType] {
+ const lastYearDay = dayjs().subtract(1, 'y')
+ const yesterday = dayjs().subtract(1, 'd')
+ return getDateRange(lastYearDay, yesterday)
+}
+
+/**
+ * 鑾峰彇鎸囧畾鏃ユ湡鐨勫紑濮嬫椂闂淬�佹埅姝㈡椂闂�
+ * @param beginDate 寮�濮嬫棩鏈�
+ * @param endDate 鎴鏃ユ湡
+ */
+export function getDateRange(
+ beginDate: dayjs.ConfigType,
+ endDate: dayjs.ConfigType
+): [string, string] {
+ return [
+ dayjs(beginDate).startOf('d').format('YYYY-MM-DD HH:mm:ss'),
+ dayjs(endDate).endOf('d').format('YYYY-MM-DD HH:mm:ss')
+ ]
+}
--
Gitblit v1.8.0