Spaces:
Sleeping
Sleeping
| // Date/time formatting utilities | |
| /** | |
| * Format ISO 8601 timestamp to user-friendly format | |
| * @param isoString ISO 8601 timestamp string | |
| * @returns Formatted date string like "Nov 10, 2025 3:45 PM" | |
| */ | |
| export function formatDateTime(isoString: string): string { | |
| const date = new Date(isoString); | |
| return date.toLocaleString('en-US', { | |
| month: 'short', | |
| day: 'numeric', | |
| year: 'numeric', | |
| hour: 'numeric', | |
| minute: '2-digit', | |
| hour12: true | |
| }); | |
| } | |
| /** | |
| * Format ISO 8601 timestamp to short format | |
| * @param isoString ISO 8601 timestamp string | |
| * @returns Formatted date string like "Nov 10, 3:45 PM" | |
| */ | |
| export function formatDateTimeShort(isoString: string): string { | |
| const date = new Date(isoString); | |
| return date.toLocaleString('en-US', { | |
| month: 'short', | |
| day: 'numeric', | |
| hour: 'numeric', | |
| minute: '2-digit', | |
| hour12: true | |
| }); | |
| } | |
| /** | |
| * Format ISO 8601 timestamp to just date | |
| * @param isoString ISO 8601 timestamp string | |
| * @returns Formatted date string like "November 10, 2025" | |
| */ | |
| export function formatDate(isoString: string): string { | |
| const date = new Date(isoString); | |
| return date.toLocaleDateString('en-US', { | |
| month: 'long', | |
| day: 'numeric', | |
| year: 'numeric' | |
| }); | |
| } | |
| /** | |
| * Format ISO 8601 timestamp to just time | |
| * @param isoString ISO 8601 timestamp string | |
| * @returns Formatted time string like "3:45 PM" | |
| */ | |
| export function formatTime(isoString: string): string { | |
| const date = new Date(isoString); | |
| return date.toLocaleTimeString('en-US', { | |
| hour: 'numeric', | |
| minute: '2-digit', | |
| hour12: true | |
| }); | |
| } | |
| /** | |
| * Format ISO 8601 timestamp to relative time | |
| * @param isoString ISO 8601 timestamp string | |
| * @returns Relative time string like "2 hours ago" or "just now" | |
| */ | |
| export function formatRelativeTime(isoString: string): string { | |
| const date = new Date(isoString); | |
| const now = new Date(); | |
| const diffMs = now.getTime() - date.getTime(); | |
| const diffSec = Math.floor(diffMs / 1000); | |
| const diffMin = Math.floor(diffSec / 60); | |
| const diffHour = Math.floor(diffMin / 60); | |
| const diffDay = Math.floor(diffHour / 24); | |
| if (diffSec < 60) { | |
| return 'just now'; | |
| } else if (diffMin < 60) { | |
| return `${diffMin} minute${diffMin !== 1 ? 's' : ''} ago`; | |
| } else if (diffHour < 24) { | |
| return `${diffHour} hour${diffHour !== 1 ? 's' : ''} ago`; | |
| } else if (diffDay < 7) { | |
| return `${diffDay} day${diffDay !== 1 ? 's' : ''} ago`; | |
| } else { | |
| return formatDate(isoString); | |
| } | |
| } | |
| /** | |
| * Convert Unix seconds timestamp to ISO 8601 string | |
| * @param unixSeconds Unix timestamp in seconds | |
| * @returns ISO 8601 timestamp string | |
| */ | |
| export function unixToISO(unixSeconds: number): string { | |
| return new Date(unixSeconds * 1000).toISOString(); | |
| } | |
| /** | |
| * Convert ISO 8601 string to Unix seconds timestamp | |
| * @param isoString ISO 8601 timestamp string | |
| * @returns Unix timestamp in seconds | |
| */ | |
| export function isoToUnix(isoString: string): number { | |
| return Math.floor(new Date(isoString).getTime() / 1000); | |
| } | |