get_current_time
Retrieve the current time in a specified format (24-hour, 12-hour, or custom) with optional seconds, ensuring accurate time-sensitive content creation.
Instructions
Get only the current time
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| format | No | Output format: '24h' (HH:MM:SS), '12h' (hh:MM:SS AM/PM), or custom format | 24h |
| include_seconds | No | Include seconds in the output |
Implementation Reference
- src/index.ts:132-160 (handler)Handler implementation for the get_current_time tool. Processes input arguments for format ('24h', '12h', or custom) and include_seconds, formats the current time using Date object, and returns the result as text content.case "get_current_time": { const format = request.params.arguments?.format || "24h"; const includeSeconds = request.params.arguments?.include_seconds !== false; let result: string; if (format === "24h") { const hours = now.getHours().toString().padStart(2, '0'); const minutes = now.getMinutes().toString().padStart(2, '0'); const seconds = now.getSeconds().toString().padStart(2, '0'); result = includeSeconds ? `${hours}:${minutes}:${seconds}` : `${hours}:${minutes}`; } else if (format === "12h") { let hours = now.getHours(); const ampm = hours >= 12 ? 'PM' : 'AM'; hours = hours % 12 || 12; const minutes = now.getMinutes().toString().padStart(2, '0'); const seconds = now.getSeconds().toString().padStart(2, '0'); result = includeSeconds ? `${hours}:${minutes}:${seconds} ${ampm}` : `${hours}:${minutes} ${ampm}`; } else { result = formatDate(now, format as string); } return { content: [{ type: "text", text: result }] }; }
- src/index.ts:58-76 (registration)Tool registration in ListToolsRequestHandler, defining the name, description, and input schema for get_current_time.{ name: "get_current_time", description: "Get only the current time", inputSchema: { type: "object", properties: { format: { type: "string", description: "Output format: '24h' (HH:MM:SS), '12h' (hh:MM:SS AM/PM), or custom format", default: "24h" }, include_seconds: { type: "boolean", description: "Include seconds in the output", default: true } } } }
- src/index.ts:167-183 (helper)Helper function formatDate used in the handler for custom format strings, replacing placeholders like 'YYYY', 'HH', etc., with date components.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; }