get_current_time
Retrieve the current date and time in multiple formats like ISO, Unix, or human-readable, with timezone support for accurate timestamping.
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 format and timezone arguments (falling back to environment variables), gets current date, formats it using formatDateTime helper, and returns the result as text content or an error message.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:117-130 (schema)Input schema definition for the get_current_time tool, specifying optional format (enum of supported formats) and timezone parameters with descriptions referencing environment defaults.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:114-131 (registration)Defines the Tool object for get_current_time, including name, description, and schema, which is used for registration.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 implements all date/time formatting logic for different formats (iso, unix, human, etc.) using Intl API and timezone support.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(); } }