get_current_datetime
Retrieve the current date and time in various formats including ISO 8601, Unix timestamp, or human-readable output, with optional timezone specification.
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 that implements the core logic for the 'get_current_datetime' tool, handling different output formats and returning the formatted current datetime.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:25-43 (registration)Registration of the 'get_current_datetime' tool in the ListTools response, including its name, description, and input schema definition.{ 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 for custom date and time formatting, used by the get_current_datetime handler and others.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; }
- src/index.ts:28-43 (schema)The input schema definition for the 'get_current_datetime' tool, specifying parameters for 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" } } } },