81 lines
2.5 KiB
TypeScript
81 lines
2.5 KiB
TypeScript
export function timeUntil(date: Date): string {
|
|
const now = new Date();
|
|
const diffMs = date.getTime() - now.getTime();
|
|
|
|
if (diffMs < 0) return 'Started';
|
|
|
|
const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
|
|
const diffDays = Math.floor(diffHours / 24);
|
|
|
|
if (diffDays > 0) {
|
|
return `${diffDays}d ${diffHours % 24}h`;
|
|
}
|
|
if (diffHours > 0) {
|
|
const diffMinutes = Math.floor((diffMs % (1000 * 60 * 60)) / (1000 * 60));
|
|
return `${diffHours}h ${diffMinutes}m`;
|
|
}
|
|
const diffMinutes = Math.floor(diffMs / (1000 * 60));
|
|
return `${diffMinutes}m`;
|
|
}
|
|
|
|
export function timeAgo(timestamp: Date | string): string {
|
|
const time = typeof timestamp === 'string' ? new Date(timestamp) : timestamp;
|
|
const diffMs = Date.now() - time.getTime();
|
|
const diffMinutes = Math.floor(diffMs / 60000);
|
|
if (diffMinutes < 1) return 'Just now';
|
|
if (diffMinutes < 60) return `${diffMinutes}m ago`;
|
|
const diffHours = Math.floor(diffMinutes / 60);
|
|
if (diffHours < 24) return `${diffHours}h ago`;
|
|
const diffDays = Math.floor(diffHours / 24);
|
|
return `${diffDays}d ago`;
|
|
}
|
|
|
|
export function getGreeting(): string {
|
|
const hour = new Date().getHours();
|
|
if (hour < 12) return 'Good morning';
|
|
if (hour < 18) return 'Good afternoon';
|
|
return 'Good evening';
|
|
}
|
|
|
|
export function formatTime(date: Date | string): string {
|
|
const d = typeof date === 'string' ? new Date(date) : date;
|
|
return d.toLocaleTimeString('en-US', {
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
});
|
|
}
|
|
|
|
export function formatDate(date: Date | string): string {
|
|
const d = typeof date === 'string' ? new Date(date) : date;
|
|
return d.toLocaleDateString('en-US', {
|
|
weekday: 'short',
|
|
month: 'short',
|
|
day: 'numeric',
|
|
});
|
|
}
|
|
|
|
export function formatFullDate(date: Date | string): string {
|
|
const d = typeof date === 'string' ? new Date(date) : date;
|
|
return d.toLocaleDateString('en-US', {
|
|
weekday: 'long',
|
|
month: 'long',
|
|
day: 'numeric',
|
|
year: 'numeric',
|
|
});
|
|
}
|
|
|
|
export function getRelativeTime(date?: Date | string): string {
|
|
if (!date) return '';
|
|
const now = new Date();
|
|
const targetDate = typeof date === 'string' ? new Date(date) : date;
|
|
const diffMs = targetDate.getTime() - now.getTime();
|
|
const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
|
|
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
|
|
|
|
if (diffMs < 0) return 'Past';
|
|
if (diffHours < 1) return 'Starting soon';
|
|
if (diffHours < 24) return `In ${diffHours}h`;
|
|
if (diffDays === 1) return 'Tomorrow';
|
|
if (diffDays < 7) return `In ${diffDays} days`;
|
|
return formatDate(targetDate);
|
|
} |