修复已知问题
This commit is contained in:
@@ -80,3 +80,53 @@ export function formatRelativeTime(timeStr) {
|
||||
return timeStr.split(' ')[0] // 或使用更美观的格式
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取天,时,分,秒
|
||||
* @param {*} dateTimeStr
|
||||
* @returns
|
||||
*/
|
||||
export const parseDateTime = dateTimeStr => {
|
||||
const date = new Date(dateTimeStr)
|
||||
|
||||
// 检查日期是否有效
|
||||
if (isNaN(date.getTime())) {
|
||||
throw new Error('Invalid date string')
|
||||
}
|
||||
|
||||
return {
|
||||
day: date.getDate(), // 月份中的第几天(1-31)
|
||||
hour: date.getHours(), // 小时(0-23)
|
||||
minute: date.getMinutes(), // 分钟(0-59)
|
||||
second: date.getSeconds() // 秒(0-59)
|
||||
}
|
||||
}
|
||||
|
||||
export const getRemainingTime = endTimeStr => {
|
||||
const now = new Date().getTime() // 当前时间戳(毫秒)
|
||||
const end = new Date(endTimeStr.replace(' ', 'T')).getTime() // 转为 ISO 格式并获取时间戳
|
||||
|
||||
if (isNaN(end)) {
|
||||
throw new Error('无效的结束时间格式')
|
||||
}
|
||||
|
||||
const diff = end - now // 剩余毫秒数
|
||||
|
||||
if (diff <= 0) {
|
||||
return { day: 0, hour: 0, minute: 0, second: 0, isExpired: true }
|
||||
}
|
||||
|
||||
const totalSeconds = Math.floor(diff / 1000)
|
||||
const day = Math.floor(totalSeconds / (24 * 3600))
|
||||
const hour = Math.floor((totalSeconds % (24 * 3600)) / 3600)
|
||||
const minute = Math.floor((totalSeconds % 3600) / 60)
|
||||
const second = totalSeconds % 60
|
||||
|
||||
return {
|
||||
day,
|
||||
hour,
|
||||
minute,
|
||||
second,
|
||||
isExpired: false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user