get_current_datetime
Retrieve the current date and time in ISO, Unix, human-readable, or custom formats, with optional timezone specification for accurate results.
Instructions
Get the current date and time
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| format | No | Output format: 'iso' (ISO 8601), 'unix' (timestamp), 'human' (readable), or a custom format string | iso |
| timezone | No | Timezone (e.g., 'UTC', 'America/New_York'). Defaults to system timezone | system |
Implementation Reference
- src/index.ts:85-107 (handler)The switch case handler that executes the get_current_datetime tool logic, formatting the current date and time based on the provided format and timezone parameters.case "get_current_datetime": { const format = request.params.arguments?.format || "iso"; const timezone = request.params.arguments?.timezone || "system"; let result: string; if (format === "iso") { result = now.toISOString(); } else if (format === "unix") { result = Math.floor(now.getTime() / 1000).toString(); } else if (format === "human") { result = now.toString(); } else { result = formatDate(now, format as string); } return { content: [{ type: "text", text: result }] }; }
- src/index.ts:28-42 (schema)Input schema defining the parameters for the get_current_datetime tool (format and timezone).inputSchema: { type: "object", properties: { format: { type: "string", description: "Output format: 'iso' (ISO 8601), 'unix' (timestamp), 'human' (readable), or a custom format string", default: "iso" }, timezone: { type: "string", description: "Timezone (e.g., 'UTC', 'America/New_York'). Defaults to system timezone", default: "system" } } }
- src/index.ts:25-43 (registration)Registration of the get_current_datetime tool in the ListTools response, including name, description, and schema.{ name: "get_current_datetime", description: "Get the current date and time", inputSchema: { type: "object", properties: { format: { type: "string", description: "Output format: 'iso' (ISO 8601), 'unix' (timestamp), 'human' (readable), or a custom format string", default: "iso" }, timezone: { type: "string", description: "Timezone (e.g., 'UTC', 'America/New_York'). Defaults to system timezone", default: "system" } } } },
- src/index.ts:167-183 (helper)Helper utility function formatDate used by the handler for custom date formatting.function formatDate(date: Date, format: string): string { const replacements: { [key: string]: string } = { 'YYYY': date.getFullYear().toString(), 'MM': (date.getMonth() + 1).toString().padStart(2, '0'), 'DD': date.getDate().toString().padStart(2, '0'), 'HH': date.getHours().toString().padStart(2, '0'), 'mm': date.getMinutes().toString().padStart(2, '0'), 'ss': date.getSeconds().toString().padStart(2, '0'), }; let result = format; for (const [key, value] of Object.entries(replacements)) { result = result.replace(new RegExp(key, 'g'), value); } return result; }