Get Current Date and Time
get-current-datetimeRetrieve the current date and time in your local timezone or any specified IANA timezone for accurate temporal data in applications.
Instructions
Returns the current date and time. Optionally specify a timezone.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| timezone | No | IANA timezone (e.g., "America/New_York", "Europe/London", "UTC") |
Implementation Reference
- src/index.ts:20-43 (handler)Handler function that fetches the current date/time, formats it in the specified timezone (or system default), and returns structured JSON data or error.
async ({ timezone }) => { const now = new Date(); const tz = timezone || Intl.DateTimeFormat().resolvedOptions().timeZone; try { const formatted = now.toLocaleString('en-US', { timeZone: tz }); return { content: [{ type: 'text', text: JSON.stringify({ iso: now.toISOString(), formatted, timezone: tz, timestamp: now.getTime() }, null, 2) }] }; } catch { return { content: [{ type: 'text', text: `Error: Invalid timezone "${timezone}". Use IANA format like "America/New_York".` }], isError: true }; } } - src/index.ts:13-19 (schema)Input schema defining the optional 'timezone' parameter with Zod validation and description.
{ title: 'Get Current Date and Time', description: 'Returns the current date and time. Optionally specify a timezone.', inputSchema: { timezone: z.string().optional().describe('IANA timezone (e.g., "America/New_York", "Europe/London", "UTC")') } }, - src/index.ts:11-44 (registration)Full registration of the 'get-current-datetime' tool with server.registerTool, specifying name, metadata/schema, and handler function.
server.registerTool( 'get-current-datetime', { title: 'Get Current Date and Time', description: 'Returns the current date and time. Optionally specify a timezone.', inputSchema: { timezone: z.string().optional().describe('IANA timezone (e.g., "America/New_York", "Europe/London", "UTC")') } }, async ({ timezone }) => { const now = new Date(); const tz = timezone || Intl.DateTimeFormat().resolvedOptions().timeZone; try { const formatted = now.toLocaleString('en-US', { timeZone: tz }); return { content: [{ type: 'text', text: JSON.stringify({ iso: now.toISOString(), formatted, timezone: tz, timestamp: now.getTime() }, null, 2) }] }; } catch { return { content: [{ type: 'text', text: `Error: Invalid timezone "${timezone}". Use IANA format like "America/New_York".` }], isError: true }; } } );