将时间转换为:几分钟前,几小时前,几年前
export const relativeTime = oldTime => {
const t = new Date(oldTime)
const diff = Date.now() - t.getTime()
const year = Math.floor(diff / (1000 * 3600 * 24 * 365))
if (year) {
return `${year}年前`
}
const month = Math.floor(diff / (1000 * 3600 * 24 * 30))
if (month) {
return `${month}月前`
}
const day = Math.floor(diff / (1000 * 3600 * 24))
if (day) {
return `${day}天前`
}
const hour = Math.floor(diff / (1000 * 3600))
if (hour) {
return `${hour}小时前`
}
const minute = Math.floor(diff / (1000 * 60))
if (minute) {
return `${minute}分钟前`
} else {
return '刚刚'
}
}
将日期格式改为:YYYY-MM-DD
export const formatDate = (dateTime) => {
const date = new Date(dateTime) // 转换成Data()
var y = date.getFullYear()
var m = date.getMonth() + 1
m = m < 10 ? '0' + m : m
var d = date.getDate()
d = d < 10 ? ('0' + d) : d
return y + '-' + m + '-' + d
}
从大数组中,选出没有在小数组中出现过的元素
export const arrSub = (allChannels, channels) => {
// 从大数组中,选出一些元素(没有在小数组中出现过)
const res = []
allChannels.forEach((item) => {
const idx = channels.findIndex(val => {
if (val.id === item.id) return true
})
if (idx === -1) {
res.push(item)
}
})
return res
}
// this.allChannels.filter((item) => !this.channels.some(index => index.name === item.name))
以rgb格式设置随机颜色
function() {let r = Math.floor(Math.random() * 256);
let g = Math.floor(Math.random() * 256);
let b = Math.floor(Math.random() * 256);
return `rgb(${r},${g},${b})`;
}