first commit
This commit is contained in:
89
src/utils/datetime.js
Normal file
89
src/utils/datetime.js
Normal file
@@ -0,0 +1,89 @@
|
||||
// 格式化时间戳为 "刚刚, 几分钟前"
|
||||
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}`;
|
||||
}
|
||||
Reference in New Issue
Block a user