Skip to main content
Glama

get_datetime

Retrieve the current date and time in multiple formats including ISO 8601, Unix timestamp, and individual components. Optionally specify a timezone for localized results.

Instructions

Get the current date and time in various formats.

Returns:

  • ISO 8601 string

  • Unix timestamp

  • Individual components (year, month, day, hour, minute, second)

  • Day of week and week number

  • Timezone information

This tool does not require any parameters.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
timezoneNoIANA timezone string (e.g., 'America/New_York', 'UTC'). Defaults to the server's local timezone.

Implementation Reference

  • The async handler function that executes the get_datetime tool logic. Creates a Date object, formats it with optional timezone, computes components (year, month, day, hour, minute, second, millisecond), day of week, week number, timezone, and UTC offset, then returns JSON output. Includes try/catch error handling.
      async ({ timezone }) => {
        try {
          const now = new Date();
    
          // Build timezone suffix for formatting
          const tzSuffix = timezone ? ` ${timezone}` : "";
          const formatted = timezone
            ? now.toLocaleString("en-US", { timeZone: timezone })
            : now.toLocaleString("en-US");
    
          const dayNames = [
            "Sunday",
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday",
          ];
    
          // Get the start of the year for week number calculation
          const startOfYear = new Date(now.getFullYear(), 0, 1);
          const pastDaysOfYear =
            (now.getTime() - startOfYear.getTime()) / 86_400_000;
          const weekNumber = Math.ceil(
            (pastDaysOfYear + startOfYear.getDay() + 1) / 7
          );
    
          return {
            content: [
              {
                type: "text" as const,
                text: JSON.stringify(
                  {
                    iso8601: now.toISOString(),
                    unixTimestamp: Math.floor(now.getTime() / 1000),
                    unixTimestampMs: now.getTime(),
                    formatted: formatted + tzSuffix,
                    components: {
                      year: now.getFullYear(),
                      month: now.getMonth() + 1,
                      day: now.getDate(),
                      hour: now.getHours(),
                      minute: now.getMinutes(),
                      second: now.getSeconds(),
                      millisecond: now.getMilliseconds(),
                    },
                    dayOfWeek: dayNames[now.getDay()],
                    weekNumber,
                    timezone: timezone || Intl.DateTimeFormat().resolvedOptions().timeZone,
                    utcOffset: `UTC${now.getTimezoneOffset() > 0 ? "-" : "+"}${String(Math.abs(Math.floor(now.getTimezoneOffset() / 60))).padStart(2, "0")}:${String(Math.abs(now.getTimezoneOffset() % 60)).padStart(2, "0")}`,
                  },
                  null,
                  2
                ),
              },
            ],
          };
        } catch (err: any) {
          return {
            content: [
              {
                type: "text" as const,
                text: `DateTime Error: ${err.message}`,
              },
            ],
            isError: true,
          };
        }
      }
    );
  • Input schema for the get_datetime tool: an optional 'timezone' string parameter (IANA timezone like 'America/New_York' or 'UTC'), described using Zod.
    {
      timezone: z
        .string()
        .optional()
        .describe(
          "IANA timezone string (e.g., 'America/New_York', 'UTC'). Defaults to the server's local timezone."
        ),
    },
  • The registerDateTimeTool function that registers 'get_datetime' on the MCP server via server.tool(). Includes tool name, description, input schema, and the handler.
    export function registerDateTimeTool(server: McpServer): void {
      server.tool(
        "get_datetime",
        `Get the current date and time in various formats.
    
    Returns:
      - ISO 8601 string
      - Unix timestamp
      - Individual components (year, month, day, hour, minute, second)
      - Day of week and week number
      - Timezone information
    
    This tool does not require any parameters.`,
        {
          timezone: z
            .string()
            .optional()
            .describe(
              "IANA timezone string (e.g., 'America/New_York', 'UTC'). Defaults to the server's local timezone."
            ),
        },
        async ({ timezone }) => {
          try {
            const now = new Date();
    
            // Build timezone suffix for formatting
            const tzSuffix = timezone ? ` ${timezone}` : "";
            const formatted = timezone
              ? now.toLocaleString("en-US", { timeZone: timezone })
              : now.toLocaleString("en-US");
    
            const dayNames = [
              "Sunday",
              "Monday",
              "Tuesday",
              "Wednesday",
              "Thursday",
              "Friday",
              "Saturday",
            ];
    
            // Get the start of the year for week number calculation
            const startOfYear = new Date(now.getFullYear(), 0, 1);
            const pastDaysOfYear =
              (now.getTime() - startOfYear.getTime()) / 86_400_000;
            const weekNumber = Math.ceil(
              (pastDaysOfYear + startOfYear.getDay() + 1) / 7
            );
    
            return {
              content: [
                {
                  type: "text" as const,
                  text: JSON.stringify(
                    {
                      iso8601: now.toISOString(),
                      unixTimestamp: Math.floor(now.getTime() / 1000),
                      unixTimestampMs: now.getTime(),
                      formatted: formatted + tzSuffix,
                      components: {
                        year: now.getFullYear(),
                        month: now.getMonth() + 1,
                        day: now.getDate(),
                        hour: now.getHours(),
                        minute: now.getMinutes(),
                        second: now.getSeconds(),
                        millisecond: now.getMilliseconds(),
                      },
                      dayOfWeek: dayNames[now.getDay()],
                      weekNumber,
                      timezone: timezone || Intl.DateTimeFormat().resolvedOptions().timeZone,
                      utcOffset: `UTC${now.getTimezoneOffset() > 0 ? "-" : "+"}${String(Math.abs(Math.floor(now.getTimezoneOffset() / 60))).padStart(2, "0")}:${String(Math.abs(now.getTimezoneOffset() % 60)).padStart(2, "0")}`,
                    },
                    null,
                    2
                  ),
                },
              ],
            };
          } catch (err: any) {
            return {
              content: [
                {
                  type: "text" as const,
                  text: `DateTime Error: ${err.message}`,
                },
              ],
              isError: true,
            };
          }
        }
      );
    }
  • src/index.ts:53-57 (registration)
    Call to registerDateTimeTool(this.server) inside registerTools() to wire up the tool on the MCP server instance.
      registerDateTimeTool(this.server);
      registerJsonParserTool(this.server);
      registerTextTransformTool(this.server);
      registerEnvironmentTool(this.server);
    }
  • Week number calculation helper logic inside the handler (computes week of year using start-of-year date arithmetic).
    // Get the start of the year for week number calculation
    const startOfYear = new Date(now.getFullYear(), 0, 1);
    const pastDaysOfYear =
      (now.getTime() - startOfYear.getTime()) / 86_400_000;
    const weekNumber = Math.ceil(
      (pastDaysOfYear + startOfYear.getDay() + 1) / 7
    );
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations, the description adequately discloses behavioral traits: it lists the return formats and states no parameters are required. However, it does not explain behavior when an invalid timezone is provided or when the timezone parameter is omitted (defaults to server time). The return format details are good.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is concise and front-loaded with the main purpose. The bullet-style list of return types is easy to scan. One minor inefficiency: the phrase 'This tool does not require any parameters' could be replaced with mentioning the optional parameter, but overall it's well-structured.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a simple tool with one optional parameter and no output schema, the description covers the core functionality and return types. However, it misses context about default timezone behavior, error handling, and the optional parameter itself. Given the low complexity, it is minimally adequate but not thorough.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters2/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 100% coverage with a clear description for the optional 'timezone' parameter. However, the tool description states 'This tool does not require any parameters,' which, while technically true, is misleading by omission because it fails to mention the optional timezone parameter. This adds confusion rather than value.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Get the current date and time in various formats.' It lists specific return types (ISO 8601, Unix timestamp, components), making the functionality unambiguous. Among siblings, no other tool provides datetime, so differentiation is clear.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description does not explicitly state when to use this tool versus alternatives. While the purpose is obvious (retrieving current datetime), no guidance is given on cases where timezone specification might be needed or that alternatives like get_environment might provide system time. Usage is implied but not articulated.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/vyshnavi-nandyala/mcp-toolkit-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server