47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
|
|
/**
|
||
|
|
* Date formatting utility functions
|
||
|
|
* Use these instead of creating inline formatDate functions in components
|
||
|
|
*/
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Format date as relative time (e.g., "Just now", "5m ago", "2h ago", "3d ago", or full date)
|
||
|
|
*/
|
||
|
|
export function formatRelativeTime(dateString: string): string {
|
||
|
|
const date = new Date(dateString);
|
||
|
|
const now = new Date();
|
||
|
|
const diffMs = now.getTime() - date.getTime();
|
||
|
|
const diffMins = Math.floor(diffMs / 60000);
|
||
|
|
const diffHours = Math.floor(diffMs / 3600000);
|
||
|
|
const diffDays = Math.floor(diffMs / 86400000);
|
||
|
|
|
||
|
|
if (diffMins < 1) return 'Just now';
|
||
|
|
if (diffMins < 60) return `${diffMins}m ago`;
|
||
|
|
if (diffHours < 24) return `${diffHours}h ago`;
|
||
|
|
if (diffDays < 7) return `${diffDays}d ago`;
|
||
|
|
return date.toLocaleDateString();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Format date with full date and time (e.g., "Jan 1, 2024, 2:30 PM")
|
||
|
|
*/
|
||
|
|
export function formatDateTime(dateString: string): string {
|
||
|
|
return new Date(dateString).toLocaleDateString('en-US', {
|
||
|
|
year: 'numeric',
|
||
|
|
month: 'short',
|
||
|
|
day: 'numeric',
|
||
|
|
hour: '2-digit',
|
||
|
|
minute: '2-digit',
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Format date only (e.g., "Jan 1, 2024")
|
||
|
|
*/
|
||
|
|
export function formatDateOnly(dateString: string): string {
|
||
|
|
return new Date(dateString).toLocaleDateString('en-US', {
|
||
|
|
month: 'short',
|
||
|
|
day: 'numeric',
|
||
|
|
year: 'numeric',
|
||
|
|
});
|
||
|
|
}
|