` component for table cells
- Cache formats in components rendering many dates
- Call `useDateTimeFormat()` at component level, not in loops
**Don't:**
- Call `useDateTimeFormat()` inside map functions
- Create new Date objects unnecessarily
- Format dates multiple times for same value
**Example:**
```typescript
// Good - hook called once per component
export function ActivityTable() {
const { formatDateTime } = useDateTimeFormat();
return data.map(item => (
| {formatDateTime(item.createdAt)} |
));
}
// Bad - hook called in map
return data.map(item => {
const { formatDateTime } = useDateTimeFormat(); // ❌
return {formatDateTime(item.createdAt)} | ;
});
```
### Error Handling
Always handle null/undefined dates:
```typescript
// Good
{lastLogin ? : 'Never'}
// Also good - component handles it
```
## Validation
Format validation enforced at multiple levels:
**Backend Validation:** JSON Schema ensures only valid formats accepted:
```json
{
"type": "string",
"enum": [
"MM/dd/yyyy",
"dd/MM/yyyy",
"yyyy-MM-dd",
"MMM dd, yyyy",
"MMMM dd, yyyy",
"dd MMM yyyy",
"dd MMMM yyyy"
]
}
```
**Frontend Validation:** Select component restricts choices to valid options.
## Troubleshooting
**Dates Not Updating:**
- React Query cache not invalidating
- Component not using formatting hook
- Hard-coded date formats still in use
**Invalid Date Display:**
- Invalid date string
- Incorrect date parsing
- Timezone issues
**Format Not Applied:**
- Component not updated to use hook
- Manual date formatting still present
- Library using hard-coded format
## Migration Guide
**Before:**
```typescript
{
new Date(timestamp).toLocaleString();
}
{
date.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric',
});
}
```
**After:**
```typescript
```
**Or using hook:**
```typescript
const { formatDate, formatDateTime } = useDateTimeFormat();
{
formatDateTime(timestamp);
}
{
formatDate(date);
}
```