isValidTimezone (src/timezone.ts)

nanoclaw · src/timezone.ts · typescript · MIT
ui, tag:knn-classified

Utility functions for validating, resolving, formatting, and parsing IANA timezones for use in date/time handling.

// Source: https://github.com/nanocoai/nanoclaw | License: MIT | Copyright 2026 Gavriel
// Provided via justclaw.it.com; keep this notice with the code.
/**
 * Check whether a timezone string is a valid IANA identifier
 * that Intl.DateTimeFormat can use.
 */
export function isValidTimezone(tz: string): boolean {
  try {
    Intl.DateTimeFormat(undefined, { timeZone: tz });
    return true;
  } catch {
    return false;
  }
}

/**
 * Return the given timezone if valid IANA, otherwise fall back to UTC.
 */
export function resolveTimezone(tz: string): string {
  return isValidTimezone(tz) ? tz : 'UTC';
}

/**
 * Convert a UTC ISO timestamp to a localized display string.
 * Uses the Intl API (no external dependencies).
 * Falls back to UTC if the timezone is invalid.
 */
export function forma

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key