get_elapsed_time
Calculate the elapsed time between two specified datetimes in customizable units (milliseconds to years) for precise duration measurement.
Instructions
Get the difference time between two datetimes (e.g. 2025-01-01 01:01:01 and 2025-01-02 02:02:02)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| from | Yes | ||
| to | Yes | ||
| unit | No | millisecond |
Implementation Reference
- src/index.ts:151-167 (handler)The asynchronous handler function that computes the elapsed time (difference) between 'from' and 'to' datetimes using dayjs.diff, with optional unit. Returns duration as string or error if invalid dates.async ({ from, to, unit = "second" }) => { const fromDateTime = dayjs(from); const toDateTime = dayjs(to); if (!fromDateTime.isValid() || !toDateTime.isValid()) { return { content: [{ type: "text", text: "Invalid format" }], isError: true, }; } /** * {@link https://day.js.org/docs/en/display/difference} */ const duration = toDateTime.diff(fromDateTime, unit, true); return { content: [{ type: "text", text: String(duration) }], }; },
- src/index.ts:134-150 (schema)Zod input schema for the tool: 'from' and 'to' as strings (datetimes), 'unit' as optional enum with default 'millisecond'.{ from: z.string(), to: z.string(), unit: z .enum([ "millisecond", "second", "minute", "hour", "day", "week", "month", "year", ]) .optional() .default("millisecond"), },
- src/index.ts:131-168 (registration)Registration of the 'get_elapsed_time' tool on the MCP server, specifying name, description, input schema, and handler function.server.tool( "get_elapsed_time", "Get the difference time between two datetimes (e.g. 2025-01-01 01:01:01 and 2025-01-02 02:02:02)", { from: z.string(), to: z.string(), unit: z .enum([ "millisecond", "second", "minute", "hour", "day", "week", "month", "year", ]) .optional() .default("millisecond"), }, async ({ from, to, unit = "second" }) => { const fromDateTime = dayjs(from); const toDateTime = dayjs(to); if (!fromDateTime.isValid() || !toDateTime.isValid()) { return { content: [{ type: "text", text: "Invalid format" }], isError: true, }; } /** * {@link https://day.js.org/docs/en/display/difference} */ const duration = toDateTime.diff(fromDateTime, unit, true); return { content: [{ type: "text", text: String(duration) }], }; }, );