|
| 1 | +import dayjs from 'dayjs'; |
| 2 | +import utc from 'dayjs/plugin/utc'; |
| 3 | +import timezone from 'dayjs/plugin/timezone'; |
| 4 | +import isSameOrAfter from 'dayjs/plugin/isSameOrAfter'; |
| 5 | +import isSameOrBefore from 'dayjs/plugin/isSameOrBefore'; |
| 6 | + |
| 7 | +// Configure dayjs plugins |
| 8 | +dayjs.extend(utc); |
| 9 | +dayjs.extend(timezone); |
| 10 | +dayjs.extend(isSameOrAfter); |
| 11 | +dayjs.extend(isSameOrBefore); |
| 12 | + |
| 13 | +/** |
| 14 | + * Format a date string to show relative time (e.g., "today", "yesterday") |
| 15 | + * or formatted date for older dates |
| 16 | + * |
| 17 | + * @param dateString - ISO date string |
| 18 | + * @returns Formatted date string |
| 19 | + * |
| 20 | + * @example |
| 21 | + * // Assuming today is 2025-11-19 |
| 22 | + * formatRelativeDate('2025-11-19T10:00:00Z') // "Today" |
| 23 | + * formatRelativeDate('2025-11-18T10:00:00Z') // "Yesterday" |
| 24 | + * formatRelativeDate('2025-11-15T10:00:00Z') // "Friday" (if within last 7 days) |
| 25 | + */ |
| 26 | +export function formatRelativeDate(dateString: string): string { |
| 27 | + const date = dayjs(dateString); |
| 28 | + const now = dayjs(); |
| 29 | + |
| 30 | + // Check if today |
| 31 | + if (date.isSame(now, 'day')) { |
| 32 | + return 'Today'; |
| 33 | + } |
| 34 | + |
| 35 | + // Check if yesterday |
| 36 | + if (date.isSame(now.subtract(1, 'day'), 'day')) { |
| 37 | + return 'Yesterday'; |
| 38 | + } |
| 39 | + |
| 40 | + // Check if tomorrow |
| 41 | + if (date.isSame(now.add(1, 'day'), 'day')) { |
| 42 | + return 'Tomorrow'; |
| 43 | + } |
| 44 | + |
| 45 | + // Check if within 7 days (past or future, excluding today, yesterday, tomorrow) |
| 46 | + if (Math.abs(date.diff(now, 'day')) < 7 && Math.abs(date.diff(now, 'day')) > 1) { |
| 47 | + return date.format('dddd'); // Day name (e.g., "Monday") |
| 48 | + } |
| 49 | + |
| 50 | + // For older dates, show formatted date |
| 51 | + return date.format('MMM D, YYYY'); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Format a date string to display full date information |
| 56 | + * |
| 57 | + * @param dateString - ISO date string |
| 58 | + * @returns Formatted date string like "Monday, November 19, 2025" |
| 59 | + */ |
| 60 | +export function formatFullDate(dateString: string): string { |
| 61 | + return dayjs(dateString).format('dddd, MMMM D, YYYY'); |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Format a date string to display short date |
| 66 | + * |
| 67 | + * @param dateString - ISO date string |
| 68 | + * @returns Formatted date string like "Nov 19, 2025" |
| 69 | + */ |
| 70 | +export function formatShortDate(dateString: string): string { |
| 71 | + return dayjs(dateString).format('MMM D, YYYY'); |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Format a time string |
| 76 | + * |
| 77 | + * @param dateString - ISO date string |
| 78 | + * @returns Formatted time string like "10:30 AM" |
| 79 | + */ |
| 80 | +export function formatTime(dateString: string): string { |
| 81 | + return dayjs(dateString).format('h:mm A'); |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Format a time string with timezone |
| 86 | + * |
| 87 | + * @param dateString - ISO date string |
| 88 | + * @param timezone - Optional timezone identifier |
| 89 | + * @returns Formatted time string with timezone like "10:30 AM PST" |
| 90 | + */ |
| 91 | +export function formatTimeWithZone(dateString: string, timezone?: string): string { |
| 92 | + if (timezone) { |
| 93 | + return dayjs(dateString).tz(timezone).format('h:mm A z'); |
| 94 | + } |
| 95 | + return dayjs(dateString).format('h:mm A'); |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Format a month and year for charts/displays |
| 100 | + * |
| 101 | + * @param dateString - ISO date string |
| 102 | + * @returns Formatted string like "Nov 2025" |
| 103 | + */ |
| 104 | +export function formatMonthYear(dateString: string): string { |
| 105 | + return dayjs(dateString).format('MMM YYYY'); |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Check if a date is in the future |
| 110 | + * |
| 111 | + * @param dateString - ISO date string |
| 112 | + * @returns True if date is in the future |
| 113 | + */ |
| 114 | +export function isFutureDate(dateString: string): boolean { |
| 115 | + return dayjs(dateString).isAfter(dayjs()); |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Check if a date is in the past |
| 120 | + * |
| 121 | + * @param dateString - ISO date string |
| 122 | + * @returns True if date is in the past |
| 123 | + */ |
| 124 | +export function isPastDate(dateString: string): boolean { |
| 125 | + return dayjs(dateString).isBefore(dayjs()); |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Check if two dates are on the same day |
| 130 | + * |
| 131 | + * @param date1 - First ISO date string |
| 132 | + * @param date2 - Second ISO date string |
| 133 | + * @returns True if dates are on the same day |
| 134 | + */ |
| 135 | +export function isSameDay(date1: string, date2: string): boolean { |
| 136 | + return dayjs(date1).isSame(dayjs(date2), 'day'); |
| 137 | +} |
0 commit comments