Skip to content

Commit c4e5419

Browse files
YunaiVgitee-org
authored andcommitted
!69 Vue3重构:公众号统计
Merge pull request !69 from kinlon92/dev
2 parents d7891fe + 51344a5 commit c4e5419

File tree

2 files changed

+482
-1
lines changed

2 files changed

+482
-1
lines changed

src/utils/dateUtils.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* 将毫秒,转换成时间字符串。例如说,xx 分钟
3+
*
4+
* @param ms 毫秒
5+
* @returns {string} 字符串
6+
*/
7+
export function getDate(ms) {
8+
const day = Math.floor(ms / (24 * 60 * 60 * 1000))
9+
const hour = Math.floor(ms / (60 * 60 * 1000) - day * 24)
10+
const minute = Math.floor(ms / (60 * 1000) - day * 24 * 60 - hour * 60)
11+
const second = Math.floor(ms / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60)
12+
if (day > 0) {
13+
return day + '天' + hour + '小时' + minute + '分钟'
14+
}
15+
if (hour > 0) {
16+
return hour + '小时' + minute + '分钟'
17+
}
18+
if (minute > 0) {
19+
return minute + '分钟'
20+
}
21+
if (second > 0) {
22+
return second + '秒'
23+
} else {
24+
return 0 + '秒'
25+
}
26+
}
27+
28+
export function beginOfDay(date) {
29+
return new Date(date.getFullYear(), date.getMonth(), date.getDate())
30+
}
31+
32+
export function endOfDay(date) {
33+
return new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59, 999)
34+
}
35+
36+
export function betweenDay(date1, date2) {
37+
date1 = convertDate(date1)
38+
date2 = convertDate(date2)
39+
// 计算差值
40+
return Math.floor((date2.getTime() - date1.getTime()) / (24 * 3600 * 1000))
41+
}
42+
43+
export function formatDate(date, fmt) {
44+
date = convertDate(date)
45+
const o = {
46+
'M+': date.getMonth() + 1, //月份
47+
'd+': date.getDate(), //日
48+
'H+': date.getHours(), //小时
49+
'm+': date.getMinutes(), //分
50+
's+': date.getSeconds(), //秒
51+
'q+': Math.floor((date.getMonth() + 3) / 3), //季度
52+
S: date.getMilliseconds() //毫秒
53+
}
54+
if (/(y+)/.test(fmt)) {
55+
// 年份
56+
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
57+
}
58+
for (const k in o) {
59+
if (new RegExp('(' + k + ')').test(fmt)) {
60+
fmt = fmt.replace(
61+
RegExp.$1,
62+
RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
63+
)
64+
}
65+
}
66+
return fmt
67+
}
68+
69+
export function addTime(date, time) {
70+
date = convertDate(date)
71+
return new Date(date.getTime() + time)
72+
}
73+
74+
export function convertDate(date) {
75+
if (typeof date === 'string') {
76+
return new Date(date)
77+
}
78+
return date
79+
}

0 commit comments

Comments
 (0)