util.js
2.15 KB
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
/**
*获取各种时间格式
* @style 1. hh:mm:ss 时:分:秒
* 2.YYYY-MM-DD 年-月-日
* 3.YYYYMMDD 年月日
* 4.YYYY-MM-DD hh:mm:ss 年-月-日 时:分:秒
* 5.YYYYMMDDhhmmss 年月日时分秒
* 6.DD 日
* 7.getDay 获取当前星期几
* 8.hhmmss 时分秒
* */
const getTime = style => {
let time = new Date()
let times = ''
//获取年月日
let yyyy = time.getFullYear()
let mm = time.getMonth()
mm = mm >= 9 ? mm + 1 : '0' + (mm + 1)
let dd = time.getDate()
dd = dd >= 10 ? dd : '0' + dd
//获取时分秒
let h = time.getHours()
h = h >= 10 ? h : '0' + h
let m = time.getMinutes()
m = m >= 10 ? m : '0' + m
let s = time.getSeconds()
s = s >= 10 ? s : '0' + s
// 获取星期
let z = time.getDay()
z = '0' + z
if (style === 'YYYY-MM-DD') {
times = yyyy + '-' + mm + '-' + dd
}
if (style === 'hh:mm:ss') {
times = h + ':' + m + ':' + s
}
if (style === 'YYYYMMDD') {
times = yyyy + '' + mm + '' + dd
}
if (style === 'YYYY') {
times = yyyy + ''
}
if (style === 'MM') {
times = mm + ''
}
if (style === 'DD') {
times = dd + ''
}
if (style === 'YYYY-MM-DD hh:mm:ss' || !style) {
times = yyyy + '-' + mm + '-' + dd + ' ' + h + ':' + m + ':' + s
}
if (style === 'YYYYMMDDhhmmss') {
times = yyyy + '' + mm + '' + dd + '' + h + '' + m + '' + s
}
if (style === 'DD') {
times = dd + ''
}
if (style === 'getDay') {
times = z + ''
}
return times
}
const getIp = () => {
return new Promise((resolve, reject) => {
uni.request({
url: 'https://api.ipify.org', // 这是一个提供获取客户端公网IP服务的简单API
method: 'GET',
success: res => {
const ip = res.data
resolve(ip)
},
fail: err => {
reject(err)
}
})
})
}
const withinSpliter = (dateStr) => {
return dateStr.replace(/[-\/]/g, '')
}
const daysBetween = (startDate, endDate) => {
const start = new Date(startDate)
const end = new Date(endDate)
const days = (end - start) / (1000 * 60 * 60 * 24)
return Math.floor(days)
}
export { getTime, getIp, withinSpliter, daysBetween }