AgentSkillsCN

date-formatting

为应用程序进行日期与时间格式化。在显示日期、时间戳,或在UI组件中格式化时间时使用此功能。应优先使用集中式工具,而非原生JS日期方法。可通过“日期”“时间”“时间戳”“formatDate”“formatTime”“toLocaleDateString”“toLocaleString”“dateUtils”“locale”“format”“显示日期”“显示时间”“日期时间”“日历”等语句触发。

SKILL.md
--- frontmatter
name: date-formatting
description: Date and time formatting for applications. Use when displaying dates, timestamps, or formatting time in UI components. Use centralized utilities instead of native JS date methods. Triggers on date, time, timestamp, formatDate, formatTime, toLocaleDateString, toLocaleString, dateUtils, locale, format, display date, show time, datetime, calendar.
allowed-tools: Read, Grep, Glob

Date Formatting

Guidelines for consistent date and time formatting across applications.

Critical Rule

Use centralized date utilities for consistency:

typescript
// ❌ AVOID - Inconsistent across locales/platforms
date.toLocaleDateString()
date.toLocaleString()
new Intl.DateTimeFormat().format(date)

// ✅ CORRECT - Use centralized utility
import { formatDate } from '@{scope}/shared/src/utils/dateUtils';
formatDate(date, { hideSeconds: true });

Quick Reference

FunctionUse CaseExample Output
formatDate(date, options?)Full date and time2024/01/15, 14:30
formatTime(date, options?)Time only14:30:45
formatRelativeDate(date)Relative displayToday, Yesterday
formatDistanceToNow(date)Time distance2 hours ago
formatDateFns(date, format?)Custom formatBased on template

Common Patterns

Transaction History

typescript
import { formatDate } from '@{scope}/shared/src/utils/dateUtils';

// Hide year if current year, hide seconds
<Text>
  {formatDate(item.createdAt, { hideTheYear: true, hideSeconds: true })}
</Text>

Custom Format

typescript
// Use format template
{formatDate(item.timestamp, { formatTemplate: 'yyyy-LL-dd HH:mm' })}

React Hook (for dynamic updates)

typescript
import { useFormatDate } from '@{scope}/kit/src/hooks/useFormatDate';

function MyComponent() {
  const { formatDate } = useFormatDate();
  return <Text>{formatDate(date, { hideSeconds: true })}</Text>;
}

Format Options

typescript
interface IFormatDateOptions {
  hideYear?: boolean;        // Always hide year
  hideMonth?: boolean;       // Always hide month
  hideTheYear?: boolean;     // Hide year if current year
  hideTheMonth?: boolean;    // Hide month if current month
  hideTimeForever?: boolean; // Hide time portion
  hideSeconds?: boolean;     // Hide seconds (HH:mm)
  formatTemplate?: string;   // Custom date-fns format
}

Detailed Guide

For comprehensive date formatting rules and examples, see date-formatting.md.

Topics covered:

  • Core utilities for date formatting
  • Available formatting functions
  • Options reference and format templates
  • Common patterns for transactions, history, and relative time
  • React hooks for dynamic updates
  • Locale-aware formatting
  • Real-world examples
  • Troubleshooting

Key Files (Example Structure)

PurposeFile Path
Core utilitiespackages/shared/src/utils/dateUtils.ts
React hookpackages/kit/src/hooks/useFormatDate.ts
Locale mappingpackages/shared/src/locale/dateLocaleMap.ts

Related Skills

  • /i18n - Internationalization and locale handling
  • /coding-patterns - General coding patterns