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
2022-01-16 23:16:00 +08:00

43 lines
1012 B
JavaScript

import _ from 'lodash'
// 获取初始化时间,默认最近一周
Date.prototype.setStart = function () {
this.setHours(0)
this.setMinutes(0)
this.setSeconds(0)
}
Date.prototype.setEnd = function () {
this.setHours(23)
this.setMinutes(59)
this.setSeconds(59)
}
// 将时间转化为秒
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)) {
ms = time.getTime()
} else if (_.isNumber(time)) {
const timeStr = _.toString(time)
const difference = timeStr.length - 13
if (difference >= 0) {
ms = timeStr.slice(0, 13)
} else {
ms = Math.floor(time * (10 ** (0 - difference)))
}
}
return ms
}
// 初始化日期
export function getNowTime (interval) {
const endTime = window.$dayJs.tz().valueOf()
const startTime = endTime - interval * 60 * 1000
return {
startTime,
endTime
}
}