类似moment的时间戳转换格式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/**
* 将「时间戳」转换为「日期时间」
* @param timeStamp 时间戳
* @param fmt 时间格式,如:「YYYY:MM:DD hh:mm」
*/
const getDate = (timeStamp = 0, fmt) => {
let date = new Date(timeStamp),
year = date.getFullYear(),
month = leftPad(date.getMonth() + 1),
day = leftPad(date.getDate()),
hour = leftPad(date.getHours()),
minus = leftPad(date.getMinutes()),
secondes = leftPad(date.getSeconds());
if(fmt){
if (/(Y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1,(year + '').substr(4 - RegExp.$1.length))
}
let o = {
'M+': month,
'D+': day,
'h+': hour,
'm+': minus,
's+': secondes
}
for (let k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
let str = o[k] + ''
fmt = fmt.replace(RegExp.$1, str)
}
}
return fmt
}
}
// 左填充
const leftPad = (string) => {
if (String(string).length < 2) return `0${string}`
return string
}