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

242 lines
6.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 时间格式化工具函数
* 统一处理时间格式化,避免重复代码
*/
/**
* 格式化日期时间
* @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('')
}