const JUST_NOW = 3000 // 3s内
const IN_SECOND = 1000 * 60 // 一分钟
const IN_MINUTE = 1000 * 60 * 60 // 一小时
const IN_HOUR = 1000 * 60 * 60 * 12 // 12小时
const IN_DAY = 1000 * 60 * 60 * 24 * 1 // 1天
const IN_MONTH = 1000 * 60 * 60 * 24 * 30 // 1个月
/**
* @param {date} time
* @returns {string}
*/
const updateTimeShow = time => {
const localTime = new Date() // 当前系统时间
const createTime = new Date(time) // 消息创建时间
const diff = localTime - createTime
if (diff <= JUST_NOW) {
return '刚刚'
} else if (diff <= IN_SECOND) {
return '1分钟内'
} else if (diff <= IN_MINUTE) {
return parseInt(diff / IN_SECOND) + '分钟前'
} else if (diff <= IN_MINUTE) {
return parseInt(diff / IN_MINUTE) + '小时前'
} else if (diff <= IN_HOUR * 2) {
const list = createTime.toString().split(' ')
const lastIndex = list[4].lastIndexOf(':')
const realtime = list[4].toString().substring(0, lastIndex)
return realtime
} else if (diff < IN_DAY * 7) {
if (diff < IN_DAY) {
return parseInt(diff / IN_HOUR) + '天前'
}
const t = createTime.toString().slice(0, 3)
switch (t) {
case 'Mon':
return '星期一'
case 'Tue':
return '星期二'
case 'Wed':
return '星期三'
case 'Thu':
return '星期四'
case 'Fri':
return '星期五'
case 'Sat':
return '星期六'
case 'Sun':
return '星期日'
}
} else if (diff < IN_MONTH * 24) {
const list = createTime.toString().split(' ')
const month = list[1]
var realtime = ''
if (diff < IN_MONTH) {
realtime += getNumberMonth(month)
} else {
realtime = list[3] + '-' + getNumberMonth(month)
}
return realtime + '-' + list[2]
}
}
const getNumberMonth = month => {
switch (month) {
case 'Jan':
return '1'
case 'Feb':
return '2'
case 'Mar':
return '3'
case 'Apr':
return '4'
case 'May':
return '5'
case 'June':
return '6'
case 'July':
return '7'
case 'Aug':
return '8'
case 'Sept':
return '9'
case 'Oct':
return '10'
case 'Nov':
return '11'
case 'Dec':
return '12'
}
}
/**
* Parse the time to string
* @param {(Object|string|number)} time
* @param {string} cFormat
* @returns {string | null}
*/
const parseTime = (time, cFormat) => {
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
let date
if (typeof time === 'object') {
date = time
} else {
if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
time = parseInt(time)
}
if ((typeof time === 'number') && (time.toString().length === 10)) {
time = time * 1000
}
date = new Date(time)
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
}
const timeString = format.replace(/{([ymdhisa])+}/g, (result, key) => {
const value = formatObj[key]
// Note: getDay() returns 0 on Sunday
if (key === 'a') {
return ['日', '一', '二', '三', '四', '五', '六'][value]
}
return value.toString().padStart(2, '0')
})
return timeString
}
/**
* @param {number} time
* @param {string} option
* @returns {string}
*/
const formatTime = (time, option) => {
if (('' + time).length === 10) {
time = parseInt(time) * 1000
} else {
time = +time
}
const d = new Date(time)
const now = Date.now()
const diff = (now - d) / 1000
if (diff < 30) {
return '刚刚'
} else if (diff < 3600) {
return Math.ceil(diff / 60) + '分钟前'
} else if (diff < 3600 * 24) {
return Math.ceil(diff / 3600) + '小时前'
} else if (diff < 3600 * 24 * 2) {
return '1天前'
}
if (option) {
return parseTime(time, option)
} else {
return (
d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分'
)
}
}