mirror of
https://github.com/rmoren97/mc-manager.git
synced 2026-03-28 17:26:47 -07:00
87 lines
2.2 KiB
TypeScript
87 lines
2.2 KiB
TypeScript
/**
|
|
* Format date for display: "Jan 15, 2026"
|
|
*/
|
|
export function formatDate(date: string | Date | null | undefined): string {
|
|
if (!date) return '—'
|
|
|
|
const d = new Date(date)
|
|
if (isNaN(d.getTime())) return '—'
|
|
|
|
return d.toLocaleDateString('en-US', {
|
|
month: 'short',
|
|
day: 'numeric',
|
|
year: 'numeric',
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Format date and time for display: "Jan 15, 2026 at 2:30 PM"
|
|
*/
|
|
export function formatDateTime(date: string | Date | null | undefined): string {
|
|
if (!date) return '—'
|
|
|
|
const d = new Date(date)
|
|
if (isNaN(d.getTime())) return '—'
|
|
|
|
return d.toLocaleDateString('en-US', {
|
|
month: 'short',
|
|
day: 'numeric',
|
|
year: 'numeric',
|
|
hour: 'numeric',
|
|
minute: '2-digit',
|
|
hour12: true,
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Format date for HTML input[type="date"]: "2026-01-15"
|
|
*/
|
|
export function formatDateForInput(date: string | Date | null | undefined): string {
|
|
if (!date) return ''
|
|
|
|
const d = new Date(date)
|
|
if (isNaN(d.getTime())) return ''
|
|
|
|
const year = d.getFullYear()
|
|
const month = String(d.getMonth() + 1).padStart(2, '0')
|
|
const day = String(d.getDate()).padStart(2, '0')
|
|
|
|
return `${year}-${month}-${day}`
|
|
}
|
|
|
|
/**
|
|
* Format file size for display: "1.5 GB", "256 MB", "12 KB"
|
|
*/
|
|
export function formatFileSize(bytes: number): string {
|
|
if (bytes === 0) return '0 B'
|
|
|
|
const units = ['B', 'KB', 'MB', 'GB', 'TB']
|
|
const i = Math.floor(Math.log(bytes) / Math.log(1024))
|
|
|
|
return `${(bytes / Math.pow(1024, i)).toFixed(i > 0 ? 1 : 0)} ${units[i]}`
|
|
}
|
|
|
|
/**
|
|
* Format relative time: "2 hours ago", "just now"
|
|
*/
|
|
export function formatRelativeTime(date: string | Date | null | undefined): string {
|
|
if (!date) return '—'
|
|
|
|
const d = new Date(date)
|
|
if (isNaN(d.getTime())) return '—'
|
|
|
|
const now = new Date()
|
|
const diffMs = now.getTime() - d.getTime()
|
|
const diffSeconds = Math.floor(diffMs / 1000)
|
|
const diffMinutes = Math.floor(diffSeconds / 60)
|
|
const diffHours = Math.floor(diffMinutes / 60)
|
|
const diffDays = Math.floor(diffHours / 24)
|
|
|
|
if (diffSeconds < 60) return 'just now'
|
|
if (diffMinutes < 60) return `${diffMinutes}m ago`
|
|
if (diffHours < 24) return `${diffHours}h ago`
|
|
if (diffDays < 7) return `${diffDays}d ago`
|
|
|
|
return formatDate(date)
|
|
}
|