format-date
Convert dates to readable formats using short, medium, long, or full styles with locale support for international applications.
Instructions
Format a date in various styles (short, medium, long, full).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| date | No | Date in ISO format (defaults to today) | |
| style | No | Date style | |
| locale | No | Locale code (e.g., "en-US", "fr-FR") |
Implementation Reference
- src/index.ts:149-184 (handler)The handler function for the 'format-date' tool. It takes date, style, and locale parameters, validates them, formats the date using toLocaleDateString with the specified style, and returns a JSON-structured response or error.async ({ date, style = 'medium', locale = 'en-US' }) => { const targetDate = date ? new Date(date) : new Date(); if (isNaN(targetDate.getTime())) { return { content: [{ type: 'text', text: 'Error: Invalid date format.' }], isError: true }; } try { const styleMap: Record<string, 'short' | 'medium' | 'long' | 'full'> = { short: 'short', medium: 'medium', long: 'long', full: 'full' }; return { content: [{ type: 'text', text: JSON.stringify({ formatted: targetDate.toLocaleDateString(locale, { dateStyle: styleMap[style] }), style, locale, iso: targetDate.toISOString() }, null, 2) }] }; } catch { return { content: [{ type: 'text', text: `Error: Invalid locale "${locale}".` }], isError: true }; } }
- src/index.ts:143-147 (schema)The input schema defined using Zod for the 'format-date' tool, specifying optional parameters for date, style (enum: short/medium/long/full), and locale.inputSchema: { date: z.string().optional().describe('Date in ISO format (defaults to today)'), style: z.enum(['short', 'medium', 'long', 'full']).optional().describe('Date style'), locale: z.string().optional().describe('Locale code (e.g., "en-US", "fr-FR")') }
- src/index.ts:138-185 (registration)The registration of the 'format-date' tool using server.registerTool, including name, metadata (title, description, inputSchema), and the handler function.server.registerTool( 'format-date', { title: 'Format Date', description: 'Format a date in various styles (short, medium, long, full).', inputSchema: { date: z.string().optional().describe('Date in ISO format (defaults to today)'), style: z.enum(['short', 'medium', 'long', 'full']).optional().describe('Date style'), locale: z.string().optional().describe('Locale code (e.g., "en-US", "fr-FR")') } }, async ({ date, style = 'medium', locale = 'en-US' }) => { const targetDate = date ? new Date(date) : new Date(); if (isNaN(targetDate.getTime())) { return { content: [{ type: 'text', text: 'Error: Invalid date format.' }], isError: true }; } try { const styleMap: Record<string, 'short' | 'medium' | 'long' | 'full'> = { short: 'short', medium: 'medium', long: 'long', full: 'full' }; return { content: [{ type: 'text', text: JSON.stringify({ formatted: targetDate.toLocaleDateString(locale, { dateStyle: styleMap[style] }), style, locale, iso: targetDate.toISOString() }, null, 2) }] }; } catch { return { content: [{ type: 'text', text: `Error: Invalid locale "${locale}".` }], isError: true }; } } );