convert_datetime_to_unix
Convert human-readable datetime strings to Unix timestamps for time calculations and system compatibility.
Instructions
Convert datetime time to unixtime (e.g. 2025-01-01 01:01:01 to 1746627290)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| time | Yes | ||
| timezone | No |
Implementation Reference
- src/index.ts:78-89 (handler)Handler function that takes a datetime string and optional timezone, parses it with dayjs, applies the timezone, and returns the Unix timestamp in milliseconds using valueOf().async ({ time, timezone }) => { const unixtime = dayjs(time).tz(getTZ(timezone)).valueOf(); return { content: [ { type: "text", text: String(unixtime), }, ], }; }, );
- src/index.ts:74-77 (schema)Zod input schema defining 'time' as required string and 'timezone' as optional string.{ time: z.string(), timezone: z.string().optional(), },
- src/index.ts:71-89 (registration)Registration of the 'convert_datetime_to_unix' tool using server.tool(), including name, description, schema, and inline handler.server.tool( "convert_datetime_to_unix", "Convert datetime time to unixtime (e.g. 2025-01-01 01:01:01 to 1746627290)", { time: z.string(), timezone: z.string().optional(), }, async ({ time, timezone }) => { const unixtime = dayjs(time).tz(getTZ(timezone)).valueOf(); return { content: [ { type: "text", text: String(unixtime), }, ], }; }, );
- src/index.ts:13-15 (helper)Helper function 'getTZ' that returns the provided timezone or guesses the local timezone using dayjs.tz.guess().const getTZ = (timezon?: string) => { return timezon || dayjs.tz.guess(); };