peixue-dev/peidu/uniapp/utils/datetime.js

242 lines
6.4 KiB
JavaScript
Raw Normal View History

/**
* 时间格式化工具函数
* 统一处理时间格式化避免重复代码
*/
/**
* 格式化日期时间
* @param {string|Date} datetime - 日期时间
* @param {string} format - 格式模板默认 'YYYY-MM-DD HH:mm:ss'
* @returns {string} - 格式化后的时间字符串
*/
export function formatDateTime(datetime, format = 'YYYY-MM-DD HH:mm:ss') {
if (!datetime) return ''
const date = new Date(datetime)
if (isNaN(date.getTime())) return ''
const year = date.getFullYear()
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 format
.replace('YYYY', year)
.replace('MM', month)
.replace('DD', day)
.replace('HH', hours)
.replace('mm', minutes)
.replace('ss', seconds)
}
/**
* 格式化日期--
* @param {string|Date} datetime - 日期时间
* @returns {string} - 格式化后的日期字符串
*/
export function formatDate(datetime) {
return formatDateTime(datetime, 'YYYY-MM-DD')
}
/**
* 格式化时间:
* @param {string|Date} datetime - 日期时间
* @returns {string} - 格式化后的时间字符串
*/
export function formatTime(datetime) {
return formatDateTime(datetime, 'HH:mm')
}
/**
* 格式化日期时间-- :
* @param {string|Date} datetime - 日期时间
* @returns {string} - 格式化后的日期时间字符串
*/
export function formatDateTimeShort(datetime) {
return formatDateTime(datetime, 'YYYY-MM-DD HH:mm')
}
/**
* 格式化为相对时间刚刚5分钟前2小时前
* @param {string|Date} datetime - 日期时间
* @returns {string} - 相对时间字符串
*/
export function formatRelativeTime(datetime) {
if (!datetime) return ''
const date = new Date(datetime)
if (isNaN(date.getTime())) return ''
const now = new Date()
const diff = now.getTime() - date.getTime()
// 小于1分钟
if (diff < 60 * 1000) {
return '刚刚'
}
// 小于1小时
if (diff < 60 * 60 * 1000) {
const minutes = Math.floor(diff / (60 * 1000))
return `${minutes}分钟前`
}
// 小于1天
if (diff < 24 * 60 * 60 * 1000) {
const hours = Math.floor(diff / (60 * 60 * 1000))
return `${hours}小时前`
}
// 小于7天
if (diff < 7 * 24 * 60 * 60 * 1000) {
const days = Math.floor(diff / (24 * 60 * 60 * 1000))
return `${days}天前`
}
// 超过7天显示具体日期
return formatDate(datetime)
}
/**
* 判断是否为今天
* @param {string|Date} datetime - 日期时间
* @returns {boolean} - 是否为今天
*/
export function isToday(datetime) {
if (!datetime) return false
const date = new Date(datetime)
if (isNaN(date.getTime())) return false
const today = new Date()
return date.getFullYear() === today.getFullYear() &&
date.getMonth() === today.getMonth() &&
date.getDate() === today.getDate()
}
/**
* 判断是否为昨天
* @param {string|Date} datetime - 日期时间
* @returns {boolean} - 是否为昨天
*/
export function isYesterday(datetime) {
if (!datetime) return false
const date = new Date(datetime)
if (isNaN(date.getTime())) return false
const yesterday = new Date()
yesterday.setDate(yesterday.getDate() - 1)
return date.getFullYear() === yesterday.getFullYear() &&
date.getMonth() === yesterday.getMonth() &&
date.getDate() === yesterday.getDate()
}
/**
* 智能格式化时间今天显示时间昨天显示"昨天"更早显示日期
* @param {string|Date} datetime - 日期时间
* @returns {string} - 格式化后的时间字符串
*/
export function formatSmartTime(datetime) {
if (!datetime) return ''
if (isToday(datetime)) {
return formatTime(datetime)
}
if (isYesterday(datetime)) {
return '昨天 ' + formatTime(datetime)
}
return formatDate(datetime)
}
/**
* 获取时间戳
* @param {string|Date} datetime - 日期时间不传则返回当前时间戳
* @returns {number} - 时间戳毫秒
*/
export function getTimestamp(datetime) {
if (!datetime) {
return Date.now()
}
const date = new Date(datetime)
if (isNaN(date.getTime())) return 0
return date.getTime()
}
/**
* 计算时间差天数
* @param {string|Date} startDate - 开始日期
* @param {string|Date} endDate - 结束日期不传则为当前时间
* @returns {number} - 天数差
*/
export function getDaysDiff(startDate, endDate) {
if (!startDate) return 0
const start = new Date(startDate)
if (isNaN(start.getTime())) return 0
const end = endDate ? new Date(endDate) : new Date()
if (isNaN(end.getTime())) return 0
const diff = end.getTime() - start.getTime()
return Math.floor(diff / (24 * 60 * 60 * 1000))
}
/**
* 解析ISO 8601格式的日期时间字符串
* @param {string} isoString - ISO 8601格式字符串2024-01-10T15:30:00
* @returns {Date} - Date对象
*/
export function parseISOString(isoString) {
if (!isoString) return null
// 处理后端返回的ISO格式可能没有时区信息
const date = new Date(isoString.replace('T', ' ').replace(/\.\d+/, ''))
if (isNaN(date.getTime())) return null
return date
}
/**
* 格式化持续时间秒转为时::
* @param {number} seconds - 秒数
* @returns {string} - 格式化后的持续时间
*/
export function formatDuration(seconds) {
if (!seconds || seconds < 0) return '00:00:00'
const hours = Math.floor(seconds / 3600)
const minutes = Math.floor((seconds % 3600) / 60)
const secs = Math.floor(seconds % 60)
return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(secs).padStart(2, '0')}`
}
/**
* 格式化持续时间秒转为中文描述
* @param {number} seconds - 秒数
* @returns {string} - 中文描述
*/
export function formatDurationChinese(seconds) {
if (!seconds || seconds < 0) return '0秒'
const hours = Math.floor(seconds / 3600)
const minutes = Math.floor((seconds % 3600) / 60)
const secs = Math.floor(seconds % 60)
const parts = []
if (hours > 0) parts.push(`${hours}小时`)
if (minutes > 0) parts.push(`${minutes}分钟`)
if (secs > 0 || parts.length === 0) parts.push(`${secs}`)
return parts.join('')
}