This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
cyber-narrator-cn-ui/src/utils/date-util.js

99 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-01-16 23:16:00 +08:00
import _ from 'lodash'
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
// 初始化日期
export function getNowTime (interval) {
2021-07-05 15:11:32 +08:00
const endTime = window.$dayJs.tz().valueOf()
const startTime = endTime - interval * 60 * 1000
return {
startTime,
endTime
2021-06-11 10:00:22 +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')
}
// 日期转换为时间戳
export function toTime (date) {
return new Date(date).getTime()
}
// 时间格式转换
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
if (isNaN(date)) {
2022-06-08 18:32:52 +08:00
d = window.$dayJs(date).valueOf() + parseInt(localStorage.getItem(storageKey.timezoneLocalOffset)) * 3600000
} 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
}
/**
* 时间戳转年月日时分秒置于数组中配合el-date-picker使用
* @param time
* @returns {number[]}
*/
export function timestampToList (time) {
// 根据地址获取当前时区
const newTimezone = window.$dayJs.tz().utcOffset() / 60
// 缓存的本地时区
const localTimezone = parseInt(localStorage.getItem(storageKey.timezoneLocalOffset))
const date = new Date(getMillisecond(time + (newTimezone - localTimezone) * 3600))
const Y = date.getFullYear()
const M = date.getMonth()
const D = date.getDate()
const H = date.getHours()
const m = date.getMinutes()
const s = date.getSeconds()
const arr = [Y, M, D, H, m, s]
return arr
}