2025-07-22 11:21:01 +08:00
|
|
|
// 格式化时间戳为 "刚刚, 几分钟前"
|
|
|
|
|
export function formatTimestamp(times) {
|
|
|
|
|
const now = new Date().getTime();
|
|
|
|
|
const date = new Date(times);
|
|
|
|
|
const diff = now - date.getTime();
|
|
|
|
|
|
|
|
|
|
if (diff > 0) {
|
|
|
|
|
// 刚刚
|
|
|
|
|
if (diff < 60000) {
|
|
|
|
|
return '刚刚';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// n分钟前
|
|
|
|
|
if (diff < 3600000) {
|
|
|
|
|
return Math.floor(diff / 60000) + '分钟前';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// n小时前
|
|
|
|
|
if (diff < 86400000) {
|
|
|
|
|
return Math.floor(diff / 3600000) + '小时前';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 昨天
|
|
|
|
|
if (diff < 172800000) {
|
|
|
|
|
return '昨天';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 前天
|
|
|
|
|
if (diff < 259200000) {
|
|
|
|
|
return '前天';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2天前 4 * 24 * 60 * 60 * 1000
|
|
|
|
|
if (diff < 345600000) {
|
|
|
|
|
return '2天前';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3天前 5 * 24 * 60 * 60 * 1000
|
|
|
|
|
if (diff < 432000000) {
|
|
|
|
|
return '3天前';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// n月n日
|
|
|
|
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
|
|
|
const day = String(date.getDate()).padStart(2, '0');
|
|
|
|
|
|
|
|
|
|
const hours = String(date.getHours()).padStart(2, '0');
|
|
|
|
|
const minutes = String(date.getMinutes()).padStart(2, '0');
|
|
|
|
|
// const seconds = String(date.getSeconds()).padStart(2, '0');
|
|
|
|
|
return month + '-' + day + ' ' + hours + ':' + minutes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 格式化时间 '2025-09-19 星期三'
|
|
|
|
|
export function getWeekStr(time) {
|
|
|
|
|
if (!time) return '';
|
|
|
|
|
|
|
|
|
|
const date = new Date(time);
|
|
|
|
|
const year = date.getFullYear();
|
|
|
|
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
|
|
|
const day = String(date.getDate()).padStart(2, '0');
|
|
|
|
|
|
|
|
|
|
const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
|
|
|
|
|
const weekday = weekdays[date.getDay()];
|
|
|
|
|
|
|
|
|
|
return `${year}-${month}-${day} ${weekday}`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取时间 start end
|
|
|
|
|
export function getDate(type) {
|
|
|
|
|
const date = new Date();
|
|
|
|
|
|
|
|
|
|
let year = date.getFullYear();
|
|
|
|
|
let month = date.getMonth() + 1;
|
|
|
|
|
let day = date.getDate();
|
|
|
|
|
|
|
|
|
|
if (type === 'start') {
|
|
|
|
|
year = year - 10;
|
|
|
|
|
} else if (type === 'end') {
|
|
|
|
|
year = year + 10;
|
|
|
|
|
}
|
|
|
|
|
month = month > 9 ? month : '0' + month;;
|
|
|
|
|
day = day > 9 ? day : '0' + day;
|
|
|
|
|
|
|
|
|
|
return `${year}-${month}-${day}`;
|
2025-08-22 21:58:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 日期格式化
|
|
|
|
|
export function parseTime(time, pattern) {
|
|
|
|
|
if (arguments.length === 0 || !time) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
const format = pattern || '{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)
|
|
|
|
|
} else if (typeof time === 'string') {
|
|
|
|
|
time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
|
|
|
|
|
}
|
|
|
|
|
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 time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
|
|
|
|
|
let value = formatObj[key]
|
|
|
|
|
// Note: getDay() returns 0 on Sunday
|
|
|
|
|
if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
|
|
|
|
|
if (result.length > 0 && value < 10) {
|
|
|
|
|
value = '0' + value
|
|
|
|
|
}
|
|
|
|
|
return value || 0
|
|
|
|
|
})
|
|
|
|
|
return time_str
|
2025-07-22 11:21:01 +08:00
|
|
|
}
|