amountFormatter.ts
9 months ago
formatTimestamp.ts
9 months ago
getRelativeTimeString.ts
9 months ago
prepareDefaultValuesFromSchema.ts
9 months ago
formatTimestamp.ts
41 lines
| 1 | /** |
| 2 | * @since 4.10.0 |
| 3 | */ |
| 4 | export function formatTimestamp(timestamp: string | null | undefined, useComma: boolean = false): string { |
| 5 | // Handle null, undefined, or empty string |
| 6 | if (!timestamp) { |
| 7 | return '—'; |
| 8 | } |
| 9 | |
| 10 | const date = new Date(timestamp); |
| 11 | |
| 12 | // Check if the date is valid |
| 13 | if (isNaN(date.getTime())) { |
| 14 | return '—'; |
| 15 | } |
| 16 | |
| 17 | const day = date.getDate(); |
| 18 | const ordinal = (day: number): string => { |
| 19 | if (day > 3 && day < 21) return 'th'; |
| 20 | switch (day % 10) { |
| 21 | case 1: |
| 22 | return 'st'; |
| 23 | case 2: |
| 24 | return 'nd'; |
| 25 | case 3: |
| 26 | return 'rd'; |
| 27 | default: |
| 28 | return 'th'; |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | const dayWithOrdinal = `${day}${ordinal(day)}`; |
| 33 | const month = date.toLocaleString('en-US', {month: 'long'}); |
| 34 | const year = date.getFullYear(); |
| 35 | const time = date.toLocaleString('en-US', {hour: 'numeric', minute: '2-digit', hour12: true}).toLowerCase(); |
| 36 | const separator = useComma ? ', ' : ' • '; |
| 37 | |
| 38 | return `${dayWithOrdinal} ${month} ${year}${separator}${time}`; |
| 39 | } |
| 40 | |
| 41 |