Skip to main content
Glama
odgrim

MCP DateTime

by odgrim

get-time-in-timezone

Get the current time in any specified timezone for accurate scheduling and coordination across different regions.

Instructions

Get the current time in a specific timezone

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
timezoneYesThe timezone to get the current time for

Implementation Reference

  • Handler function that validates the input timezone and returns the current time in that timezone using the helper function getCurrentTimeInTimezone, or an error if invalid.
    async (args) => {
      if (!isValidTimezone(args.timezone)) {
        return {
          content: [{ 
            type: "text", 
            text: `Error: Invalid timezone "${args.timezone}". Use the "list-timezones" tool to see available options.`
          }],
          isError: true
        };
      }
      
      return {
        content: [{ 
          type: "text", 
          text: `The current time in ${args.timezone} is ${getCurrentTimeInTimezone(args.timezone)}`
        }]
      };
    }
  • Input schema for the tool, defining a required 'timezone' string parameter.
    {
      timezone: z.string().describe("The timezone to get the current time for")
    },
  • src/server.ts:46-70 (registration)
    Registration of the 'get-time-in-timezone' tool on the MCP server, including name, description, schema, and handler.
    server.tool(
      "get-time-in-timezone",
      "Get the current time in a specific timezone",
      {
        timezone: z.string().describe("The timezone to get the current time for")
      },
      async (args) => {
        if (!isValidTimezone(args.timezone)) {
          return {
            content: [{ 
              type: "text", 
              text: `Error: Invalid timezone "${args.timezone}". Use the "list-timezones" tool to see available options.`
            }],
            isError: true
          };
        }
        
        return {
          content: [{ 
            type: "text", 
            text: `The current time in ${args.timezone} is ${getCurrentTimeInTimezone(args.timezone)}`
          }]
        };
      }
    );
  • Helper function to validate if a given timezone string is valid using Intl.DateTimeFormat.
    export function isValidTimezone(timezone: string): boolean {
      try {
        // Try to use the timezone with Intl.DateTimeFormat
        Intl.DateTimeFormat(undefined, { timeZone: timezone });
        return true;
      } catch (error) {
        return false;
      }
    }
  • Core helper function that computes and formats the current date-time in the specified timezone using Intl APIs, returning an ISO8601-like string.
    export function getCurrentTimeInTimezone(timezone: string): string {
      try {
        const date = new Date();
        
        // Create a formatter that includes the timezone
        const options: Intl.DateTimeFormatOptions = {
          timeZone: timezone,
          timeZoneName: 'short'
        };
        
        // Get the timezone offset from the formatter
        const formatter = new Intl.DateTimeFormat('en-US', options);
        const formattedDate = formatter.format(date);
        const timezonePart = formattedDate.split(' ').pop() || '';
        
        // Format the date in ISO8601 format with the timezone
        // First get the date in the specified timezone
        const tzFormatter = new Intl.DateTimeFormat('en-US', {
          timeZone: timezone,
          year: 'numeric',
          month: '2-digit',
          day: '2-digit',
          hour: '2-digit',
          minute: '2-digit',
          second: '2-digit',
          hour12: false,
          fractionalSecondDigits: 3
        });
        
        const parts = tzFormatter.formatToParts(date);
        const dateParts: Record<string, string> = {};
        
        parts.forEach(part => {
          if (part.type !== 'literal') {
            dateParts[part.type] = part.value;
          }
        });
        
        // Format as YYYY-MM-DDTHH:MM:SS.sss±HH:MM (ISO8601)
        const isoDate = `${dateParts.year}-${dateParts.month}-${dateParts.day}T${dateParts.hour}:${dateParts.minute}:${dateParts.second}.${dateParts.fractionalSecond || '000'}`;
        
        // For proper ISO8601, we need to add the timezone offset
        // We can use the Intl.DateTimeFormat to get the timezone offset
        const tzOffset = new Date().toLocaleString('en-US', { timeZone: timezone, timeZoneName: 'longOffset' }).split(' ').pop() || '';
        
        // Format the final ISO8601 string
        return `${isoDate}${tzOffset.replace('GMT', '')}`;
      } catch (error) {
        console.error(`Error formatting time for timezone ${timezone}:`, error);
        return 'Invalid timezone';
      }
    } 

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/odgrim/mcp-datetime'

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