83 lines
2.2 KiB
JavaScript
83 lines
2.2 KiB
JavaScript
export function formatTime(date) {
|
|
if (!date) return ''
|
|
if (typeof date === 'string') {
|
|
date = new Date(date)
|
|
}
|
|
const hours = String(date.getHours()).padStart(2, '0')
|
|
const minutes = String(date.getMinutes()).padStart(2, '0')
|
|
return `${hours}:${minutes}`
|
|
}
|
|
|
|
export function formatDate(date) {
|
|
if (!date) return ''
|
|
if (typeof date === 'string') {
|
|
date = new Date(date)
|
|
}
|
|
const year = date.getFullYear()
|
|
const month = String(date.getMonth() + 1).padStart(2, '0')
|
|
const day = String(date.getDate()).padStart(2, '0')
|
|
return `${year}-${month}-${day}`
|
|
}
|
|
|
|
export function formatDateTime(date) {
|
|
if (!date) return ''
|
|
if (typeof date === 'string') {
|
|
date = new Date(date)
|
|
}
|
|
return `${formatDate(date)} ${formatTime(date)}:${String(date.getSeconds()).padStart(2, '0')}`
|
|
}
|
|
|
|
export function formatDuration(minutes) {
|
|
if (!minutes || minutes < 0) return '0分钟'
|
|
|
|
const hours = Math.floor(minutes / 60)
|
|
const mins = Math.round(minutes % 60)
|
|
|
|
if (hours === 0) {
|
|
return `${mins}分钟`
|
|
}
|
|
if (mins === 0) {
|
|
return `${hours}小时`
|
|
}
|
|
return `${hours}小时${mins}分`
|
|
}
|
|
|
|
export function formatTimerDisplay(totalSeconds) {
|
|
const hours = Math.floor(totalSeconds / 3600)
|
|
const minutes = Math.floor((totalSeconds % 3600) / 60)
|
|
const seconds = totalSeconds % 60
|
|
return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`
|
|
}
|
|
|
|
export function getGreeting() {
|
|
const hour = new Date().getHours()
|
|
if (hour < 6) return '凌晨好'
|
|
if (hour < 12) return '早上好'
|
|
if (hour < 14) return '中午好'
|
|
if (hour < 18) return '下午好'
|
|
return '晚上好'
|
|
}
|
|
|
|
export function isToday(dateStr) {
|
|
return dateStr === formatDate(new Date())
|
|
}
|
|
|
|
export function isYesterday(dateStr) {
|
|
const yesterday = new Date()
|
|
yesterday.setDate(yesterday.getDate() - 1)
|
|
return dateStr === formatDate(yesterday)
|
|
}
|
|
|
|
export function daysBetween(date1, date2) {
|
|
const d1 = new Date(date1)
|
|
const d2 = new Date(date2)
|
|
const diffTime = Math.abs(d2 - d1)
|
|
return Math.ceil(diffTime / (1000 * 60 * 60 * 24))
|
|
}
|
|
|
|
export function getWeekday(dateStr) {
|
|
const weekdays = ['日', '一', '二', '三', '四', '五', '六']
|
|
const date = new Date(dateStr)
|
|
return weekdays[date.getDay()]
|
|
}
|