优化日程界面
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
export function pad(n) {
|
||||
return String(n).padStart(2, '0')
|
||||
}
|
||||
|
||||
export function toDate(value) {
|
||||
if (!value) return null
|
||||
const d = value instanceof Date ? value : new Date(value)
|
||||
return Number.isNaN(d.getTime()) ? null : d
|
||||
}
|
||||
|
||||
export function formatDate(value, withTime = false) {
|
||||
const d = toDate(value)
|
||||
if (!d) return ''
|
||||
const date = `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`
|
||||
if (!withTime) return date
|
||||
return `${date} ${pad(d.getHours())}:${pad(d.getMinutes())}`
|
||||
}
|
||||
|
||||
export function formatRelativeTime(value) {
|
||||
const d = toDate(value)
|
||||
if (!d) return ''
|
||||
const now = new Date()
|
||||
const diff = now.getTime() - d.getTime()
|
||||
const minute = 60 * 1000
|
||||
const hour = 60 * minute
|
||||
const day = 24 * hour
|
||||
|
||||
if (diff < minute) return '刚刚'
|
||||
if (diff < hour) return `${Math.floor(diff / minute)} 分钟前`
|
||||
if (diff < day) return `${Math.floor(diff / hour)} 小时前`
|
||||
if (diff < 7 * day) return `${Math.floor(diff / day)} 天前`
|
||||
return formatDate(d)
|
||||
}
|
||||
|
||||
export function isSameDay(a, b) {
|
||||
const da = toDate(a)
|
||||
const db = toDate(b)
|
||||
if (!da || !db) return false
|
||||
return (
|
||||
da.getFullYear() === db.getFullYear() &&
|
||||
da.getMonth() === db.getMonth() &&
|
||||
da.getDate() === db.getDate()
|
||||
)
|
||||
}
|
||||
|
||||
export function isToday(value) {
|
||||
return isSameDay(value, new Date())
|
||||
}
|
||||
|
||||
export function isTomorrow(value) {
|
||||
const d = toDate(value)
|
||||
if (!d) return false
|
||||
const tomorrow = new Date()
|
||||
tomorrow.setDate(tomorrow.getDate() + 1)
|
||||
return isSameDay(d, tomorrow)
|
||||
}
|
||||
|
||||
export function dayLabel(value) {
|
||||
if (isToday(value)) return '今天'
|
||||
if (isTomorrow(value)) return '明天'
|
||||
return formatDate(value)
|
||||
}
|
||||
|
||||
export function startOfDay(value = new Date()) {
|
||||
const d = toDate(value) || new Date()
|
||||
return new Date(d.getFullYear(), d.getMonth(), d.getDate())
|
||||
}
|
||||
|
||||
export function endOfDay(value = new Date()) {
|
||||
const d = startOfDay(value)
|
||||
d.setHours(23, 59, 59, 999)
|
||||
return d
|
||||
}
|
||||
Reference in New Issue
Block a user