getZonedParts (timezones/zoned-time.ts)

appwrite · apps/console/src/lib/timezones/zoned-time.ts · typescript · BSD-3-Clause
ui, tag:knn-classified

Functions to convert and compare zoned date-time parts, format offsets, and validate time zones in the browser.

// Source: https://github.com/appwrite/appwrite | License: BSD-3-Clause | Copyright 2019 Appwrite (https://appwrite.io) and individual contributors
// Provided via justclaw.it.com; keep this notice with the code.
export type ZonedDateTimeParts = {
  year: number
  month: number
  day: number
  hour: number
  minute: number
  second: number
}

function partMap(date: Date, timeZone: string): Record<string, string> {
  const parts = new Intl.DateTimeFormat('en-US', {
    timeZone,
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    hourCycle: 'h23',
  }).formatToParts(date)

  const map: Record<string, string> = {}
  for (const part of parts) {
    if (part.type !== 'literal') map[part.type] = part.value
  }
  r

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

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

Get a free API key