get_current_time
Retrieve current date and time in multiple formats like ISO, Unix, or custom. Supports timezone adjustments for accurate timestamp generation.
Instructions
Get the current date and time in various formats
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| format | No | Output format for the datetime (optional, defaults to "iso" from env) | |
| timezone | No | Timezone to use (optional, defaults to "UTC" from env) |
Implementation Reference
- src/index.ts:349-377 (handler)Executes the get_current_time tool: parses arguments, formats current date/time using helpers, returns as text content or error.if (name === "get_current_time") { const format = (args as any)?.format || DATETIME_FORMAT; const timezone = (args as any)?.timezone || TIMEZONE; const now = new Date(); try { const formattedDate = formatDateTime(now, format, timezone); return { content: [ { type: "text", text: formattedDate, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error formatting date: ${ error instanceof Error ? error.message : String(error) }`, }, ], isError: true, }; } }
- src/index.ts:114-131 (schema)Defines the tool schema including input parameters for format and timezone with enums and descriptions.const GET_CURRENT_TIME_TOOL: Tool = { name: "get_current_time", description: "Get the current date and time in various formats", inputSchema: { type: "object", properties: { format: { type: "string", enum: ["iso", "unix", "unix_ms", "human", "date", "time", "custom"], description: `Output format for the datetime (optional, defaults to "${DATETIME_FORMAT}" from env)`, }, timezone: { type: "string", description: `Timezone to use (optional, defaults to "${TIMEZONE}" from env)`, }, }, }, };
- src/index.ts:319-321 (registration)Registers the get_current_time tool by including it in the ListTools response.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [GET_CURRENT_TIME_TOOL], }));
- src/index.ts:193-240 (helper)Core helper function that formats the Date object into the requested format using Intl API or custom logic.function formatDateTime(date: Date, format: string, timezone: string): string { // Create date in the specified timezone const options: Intl.DateTimeFormatOptions = { timeZone: timezone, }; switch (format) { case "iso": return date.toISOString(); case "unix": return Math.floor(date.getTime() / 1000).toString(); case "unix_ms": return date.getTime().toString(); case "human": options.weekday = "short"; options.year = "numeric"; options.month = "short"; options.day = "numeric"; options.hour = "2-digit"; options.minute = "2-digit"; options.second = "2-digit"; options.hour12 = true; return date.toLocaleString("en-US", options); case "date": options.year = "numeric"; options.month = "2-digit"; options.day = "2-digit"; return date.toLocaleDateString("en-CA", options); // en-CA gives YYYY-MM-DD format case "time": options.hour = "2-digit"; options.minute = "2-digit"; options.second = "2-digit"; options.hour12 = false; return date.toLocaleTimeString("en-US", options); case "custom": // Simple custom format implementation return formatCustomDate(date, DATE_FORMAT_STRING, timezone); default: return date.toISOString(); } }
- src/index.ts:265-296 (helper)Helper for custom date formatting by replacing tokens like YYYY, MM, etc., in the format string.function formatCustomDate( date: Date, formatString: string, timezone: string ): string { const options: Intl.DateTimeFormatOptions = { timeZone: timezone, year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit", hour12: false, }; const parts = new Intl.DateTimeFormat("en-US", options).formatToParts(date); const partsMap: Record<string, string> = {}; parts.forEach((part) => { partsMap[part.type] = part.value; }); return formatString .replace(/YYYY/g, partsMap.year || "") .replace(/YY/g, (partsMap.year || "").slice(-2)) .replace(/MM/g, partsMap.month || "") .replace(/DD/g, partsMap.day || "") .replace(/HH/g, partsMap.hour || "") .replace(/mm/g, partsMap.minute || "") .replace(/ss/g, partsMap.second || ""); }