relative_time
Calculate how much time has passed or will pass from a specific date to now, displaying results like '2 days ago' or 'in 3 hours' for clear temporal context.
Instructions
Get the relative time from now.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| time | Yes | The time to get the relative time from now. Format: YYYY-MM-DD HH:mm:ss |
Implementation Reference
- src/index.ts:171-173 (handler)The core handler function that computes the relative time from the given time string using dayjs's fromNow() method.function getRelativeTime(time: string) { return dayjs(time).fromNow(); }
- src/index.ts:57-73 (handler)The switch case handler in CallToolRequestSchema that validates arguments, calls getRelativeTime, and returns the formatted response.case 'relative_time': { if (!checkRelativeTimeArgs(args)) { throw new Error(`Invalid arguments for tool: [${name}]`); } const time = args.time; const result = getRelativeTime(time); return { success: true, content: [ { type: 'text', text: result, }, ], }; }
- src/tools.ts:35-48 (schema)Defines the tool metadata including name, description, and input schema for 'relative_time'.export const RELATIVE_TIME: Tool = { name: 'relative_time', description: 'Get the relative time from now.', inputSchema: { type: 'object', properties: { time: { type: 'string', description: 'The time to get the relative time from now. Format: YYYY-MM-DD HH:mm:ss', }, }, required: ['time'], }, };
- src/index.ts:30-34 (registration)Registers the relative_time tool by including RELATIVE_TIME in the tools list returned by ListToolsRequest handler.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [CURRENT_TIME, RELATIVE_TIME, DAYS_IN_MONTH, GET_TIMESTAMP, CONVERT_TIME, GET_WEEK_YEAR], }; });
- src/index.ts:213-220 (helper)Helper function to validate the input arguments for the relative_time tool.function checkRelativeTimeArgs(args: unknown): args is { time: string } { return ( typeof args === 'object' && args !== null && 'time' in args && typeof args.time === 'string' ); }