50 lines
1.4 KiB
JavaScript
50 lines
1.4 KiB
JavaScript
/**
|
|
* 补全 URL 协议头,防止无效跳转
|
|
*/
|
|
export function formatUrl(url) {
|
|
if (!url || typeof url !== 'string') return '#'
|
|
if (url.startsWith('http://') || url.startsWith('https://')) return url
|
|
return `http://${url}`
|
|
}
|
|
|
|
/**
|
|
* 将日期字符串或时间戳转换为 YYYY-MM-DD 格式
|
|
*/
|
|
export function formatDate(time) {
|
|
if (!time || time === '0') return '-'
|
|
|
|
if (typeof time === 'string' && time.includes('-')) {
|
|
return time.slice(0, 10)
|
|
}
|
|
|
|
const timestamp = Number(time)
|
|
if (!isNaN(timestamp) && timestamp > 0) {
|
|
const date = new Date(timestamp > 9999999999 ? timestamp : timestamp * 1000)
|
|
if (!isNaN(date.getTime())) {
|
|
const y = date.getFullYear()
|
|
const m = (date.getMonth() + 1).toString().padStart(2, '0')
|
|
const d = date.getDate().toString().padStart(2, '0')
|
|
return `${y}-${m}-${d}`
|
|
}
|
|
}
|
|
|
|
return '-'
|
|
}
|
|
|
|
/**
|
|
* 将时间转换为 MM-DD HH:mm 格式
|
|
*/
|
|
export function formatDateTime(time) {
|
|
if (!time) return '-'
|
|
if (typeof time === 'string' && time.includes('-')) return time
|
|
|
|
const date = new Date(time)
|
|
if (isNaN(date.getTime())) return '-'
|
|
|
|
const m = (date.getMonth() + 1).toString().padStart(2, '0')
|
|
const d = date.getDate().toString().padStart(2, '0')
|
|
const h = date.getHours().toString().padStart(2, '0')
|
|
const i = date.getMinutes().toString().padStart(2, '0')
|
|
return `${m}-${d} ${h}:${i}`
|
|
}
|