im-ui-mobile
Version:
A Vue3.0 + Typescript instant messaging component library for Uniapp
153 lines (139 loc) • 4.8 kB
JavaScript
// 日期时间工具函数
/**
* 将时间戳转换为相对时间文本
* @param {number|string|Date} timeStamp - 时间戳
* @param {boolean} simple - 是否使用简单格式
* @returns {string} 格式化后的时间文本
*/
const toTimeText = (timeStamp, simple = false) => {
const dateTime = new Date(timeStamp);
const currentTime = Date.now(); // 当前时间戳
const timeDiff = currentTime - dateTime.getTime(); // 与当前时间误差
let timeText = '';
if (timeDiff <= 60000) { // 一分钟内
timeText = '刚刚';
} else if (timeDiff > 60000 && timeDiff < 3600000) {
// 1小时内
timeText = Math.floor(timeDiff / 60000) + '分钟前';
} else if (timeDiff >= 3600000 && timeDiff < 86400000 && !isYesterday(dateTime)) {
// 今日
timeText = formatDateTime(dateTime).substr(11, 5);
} else if (isYesterday(dateTime)) {
// 昨天
timeText = '昨天' + formatDateTime(dateTime).substr(11, 5);
} else if (isYear(dateTime)) {
// 今年
timeText = formatDateTime(dateTime).substr(5, simple ? 5 : 14);
} else {
// 不属于今年
timeText = formatDateTime(dateTime);
if (simple) {
timeText = timeText.substr(2, 8);
}
}
return timeText;
}
/**
* 判断日期是否是昨天
* @param {Date} date - 日期对象
* @returns {boolean} 是否是昨天
*/
const isYesterday = (date) => {
const yesterday = new Date(Date.now() - 1000 * 60 * 60 * 24);
return yesterday.getFullYear() === date.getFullYear() &&
yesterday.getMonth() === date.getMonth() &&
yesterday.getDate() === date.getDate();
}
/**
* 判断日期是否在今年
* @param {Date} date - 日期对象
* @returns {boolean} 是否在今年
*/
const isYear = (date) => {
return date.getFullYear() === new Date().getFullYear();
}
/**
* 格式化日期时间为字符串
* @param {string|Date} date - 日期对象或字符串
* @returns {string} 格式化后的日期时间字符串
*/
const formatDateTime = (date) => {
if (date === '' || !date) {
return '';
}
const dateObject = new Date(date);
// 检查日期是否有效
if (isNaN(dateObject.getTime())) {
return '';
}
const y = dateObject.getFullYear();
const m = dateObject.getMonth() + 1;
const month = m < 10 ? ('0' + m) : m;
const d = dateObject.getDate();
const day = d < 10 ? ('0' + d) : d;
const h = dateObject.getHours();
const hour = h < 10 ? ('0' + h) : h;
const minute = dateObject.getMinutes();
const min = minute < 10 ? ('0' + minute) : minute;
const second = dateObject.getSeconds();
const sec = second < 10 ? ('0' + second) : second;
return `${y}/${month}/${day} ${hour}:${min}:${sec}`;
}
/**
* 获取更精确的相对时间描述
* @param {number|string|Date} timeStamp - 时间戳
* @returns {string} 精确的相对时间文本
*/
const toPreciseTimeText = (timeStamp) => {
const dateTime = new Date(timeStamp);
const currentTime = Date.now();
const timeDiff = currentTime - dateTime.getTime();
if (timeDiff < 60000) {
return `${Math.floor(timeDiff / 1000)}秒前`;
} else if (timeDiff < 3600000) {
const minutes = Math.floor(timeDiff / 60000);
return `${minutes}分钟前`;
} else if (timeDiff < 86400000) {
const hours = Math.floor(timeDiff / 3600000);
return `${hours}小时前`;
} else if (timeDiff < 604800000) {
const days = Math.floor(timeDiff / 86400000);
return `${days}天前`;
} else {
return formatDateTime(dateTime);
}
}
/**
* 获取聊天界面常用的时间显示格式
* @param {number|string|Date} timeStamp - 时间戳
* @returns {string} 聊天时间显示文本
*/
const toChatTimeText = (timeStamp) => {
const dateTime = new Date(timeStamp);
const currentTime = Date.now();
const timeDiff = currentTime - dateTime.getTime();
// 今天内的消息
if (timeDiff < 86400000 && !isYesterday(dateTime)) {
return formatDateTime(dateTime).substr(11, 5);
}
// 昨天的消息
else if (isYesterday(dateTime)) {
return '昨天 ' + formatDateTime(dateTime).substr(11, 5);
}
// 今年内的消息
else if (isYear(dateTime)) {
return formatDateTime(dateTime).substr(5, 5); // MM/dd
}
// 往年的消息
else {
return formatDateTime(dateTime).substr(2, 8); // yy/MM/dd
}
}
export default {
toTimeText,
toPreciseTimeText,
toChatTimeText,
isYesterday,
isYear,
formatDateTime
};