2022-01-16 23:16:00 +08:00
|
|
|
|
import _ from 'lodash'
|
2022-04-19 13:11:26 +08:00
|
|
|
|
import { storageKey } from '@/utils/constants'
|
2022-06-08 15:31:41 +08:00
|
|
|
|
|
2022-01-16 23:16:00 +08:00
|
|
|
|
// 将时间转化为秒
|
|
|
|
|
|
export function getSecond (time) {
|
|
|
|
|
|
const ms = getMillisecond(time)
|
|
|
|
|
|
return ms ? Math.floor(ms / 1000) : null
|
|
|
|
|
|
}
|
|
|
|
|
|
// 将时间转化为毫秒
|
|
|
|
|
|
export function getMillisecond (time) {
|
|
|
|
|
|
let ms = null
|
|
|
|
|
|
if (_.isDate(time)) {
|
2022-06-08 15:31:41 +08:00
|
|
|
|
ms = window.$dayJs.tz(new Date(time)).valueOf()
|
2022-01-16 23:16:00 +08:00
|
|
|
|
} else if (_.isNumber(time)) {
|
|
|
|
|
|
const timeStr = _.toString(time)
|
|
|
|
|
|
const difference = timeStr.length - 13
|
|
|
|
|
|
if (difference >= 0) {
|
2022-06-08 15:31:41 +08:00
|
|
|
|
ms = window.$dayJs.tz(new Date(Number(timeStr.slice(0, 13)))).valueOf()
|
2022-01-16 23:16:00 +08:00
|
|
|
|
} else {
|
2022-06-08 15:31:41 +08:00
|
|
|
|
ms = window.$dayJs.tz(new Date(Math.floor(time * (10 ** (0 - difference))))).valueOf()
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (_.isString(time)) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
ms = window.$dayJs.tz(new Date(time)).valueOf()
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
ms = null
|
2021-06-11 10:00:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-06-08 15:31:41 +08:00
|
|
|
|
return ms || null
|
2021-06-11 10:00:22 +08:00
|
|
|
|
}
|
2022-01-16 23:16:00 +08:00
|
|
|
|
// 初始化日期
|
2021-06-21 18:41:17 +08:00
|
|
|
|
export function getNowTime (interval) {
|
2021-07-05 15:11:32 +08:00
|
|
|
|
const endTime = window.$dayJs.tz().valueOf()
|
2021-06-21 18:41:17 +08:00
|
|
|
|
const startTime = endTime - interval * 60 * 1000
|
|
|
|
|
|
return {
|
|
|
|
|
|
startTime,
|
|
|
|
|
|
endTime
|
2021-06-11 10:00:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-03-05 20:25:03 +08:00
|
|
|
|
// 日期格式转换
|
|
|
|
|
|
export function rTime (date) {
|
2022-03-09 18:58:36 +08:00
|
|
|
|
return window.$dayJs.tz(new Date(date)).format('MM-DD HH:mm')
|
2022-03-03 18:01:26 +08:00
|
|
|
|
}
|
2022-04-19 13:11:26 +08:00
|
|
|
|
// 时间格式转换
|
|
|
|
|
|
export function dateFormat (date, format = 'YYYY-MM-DD HH:mm:ss') {
|
|
|
|
|
|
let d = date
|
2022-06-08 18:32:52 +08:00
|
|
|
|
// date不是数字,则视为utc时区的时间字符串,例如2022-02-22 22:22
|
2022-04-19 13:11:26 +08:00
|
|
|
|
if (isNaN(date)) {
|
2022-06-08 18:32:52 +08:00
|
|
|
|
d = window.$dayJs(date).valueOf() + parseInt(localStorage.getItem(storageKey.timezoneLocalOffset)) * 3600000
|
2022-04-19 13:11:26 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
d = getMillisecond(date)
|
|
|
|
|
|
}
|
|
|
|
|
|
d = window.$dayJs(d).tz().format(format)
|
|
|
|
|
|
return d
|
|
|
|
|
|
}
|
|
|
|
|
|
// 时间格式转换,使用appearance的时间格式
|
|
|
|
|
|
export function dateFormatByAppearance (date) {
|
|
|
|
|
|
return dateFormat(date, localStorage.getItem(storageKey.dateFormat))
|
|
|
|
|
|
}
|
2022-06-08 18:32:52 +08:00
|
|
|
|
// 带时区的日期转为utc日期
|
|
|
|
|
|
export function dateFormatToUTC (date, format = 'YYYY-MM-DD HH:mm:ss') {
|
|
|
|
|
|
let d = date
|
|
|
|
|
|
// date不是数字,则视为utc时区的时间字符串,例如2022-02-22 22:22
|
|
|
|
|
|
if (isNaN(date)) {
|
|
|
|
|
|
d = window.$dayJs(date).valueOf() - parseInt(localStorage.getItem(storageKey.timezoneLocalOffset)) * 3600000
|
|
|
|
|
|
} else {
|
|
|
|
|
|
d = getMillisecond(date)
|
|
|
|
|
|
}
|
|
|
|
|
|
d = window.$dayJs(d).tz().format(format)
|
|
|
|
|
|
return d
|
|
|
|
|
|
}
|