UNPKG

xbccs

Version:

customer service

66 lines (65 loc) 2.38 kB
export function isToday(timestamp) { if (isNaN(timestamp) || timestamp <= 0) { return false; } if (timestamp.toString().length === 10) { timestamp *= 1000; } const today = new Date(); const date = new Date(timestamp); return date.getDate() === today.getDate() && date.getMonth() === today.getMonth() && date.getFullYear() === today.getFullYear(); } export function toTimeStr(timestamp) { if (isNaN(timestamp) || timestamp <= 0) { return ""; } if (timestamp.toString().length === 10) { timestamp *= 1000; } const date = new Date(timestamp); const year = date.getFullYear(); const month = (date.getMonth() + 1).toString().padStart(2, '0'); const day = date.getDate().toString().padStart(2, '0'); const hours = date.getHours().toString().padStart(2, '0'); const minutes = date.getMinutes().toString().padStart(2, '0'); const seconds = date.getSeconds().toString().padStart(2, '0'); return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; } export function toTimeAgo(timestamp) { if (isNaN(timestamp) || timestamp <= 0) { return ""; } if (timestamp.toString().length === 10) { timestamp *= 1000; } const now = Date.now(); if (timestamp > now) { return ""; } const diff = now - timestamp; const oneMinute = 60 * 1000; if (diff <= oneMinute) { return "刚刚"; } const currentDate = new Date(now); const todayStart = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate()).getTime(); const targetDate = new Date(timestamp); const targetDayStart = new Date(targetDate.getFullYear(), targetDate.getMonth(), targetDate.getDate()).getTime(); const dayDiff = (todayStart - targetDayStart) / 86400000; if (dayDiff === 0) { const hours = targetDate.getHours().toString().padStart(2, "0"); const minutes = targetDate.getMinutes().toString().padStart(2, "0"); return `${hours}:${minutes}`; } else if (dayDiff === 1) { return "昨天"; } else { const year = targetDate.getFullYear(); const month = (targetDate.getMonth() + 1).toString().padStart(2, "0"); const day = targetDate.getDate().toString().padStart(2, "0"); return `${year}/${month}/${day}`; } }