CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-internationalized--date

Internationalized calendar, date, and time manipulation utilities with support for 13 international calendar systems

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

date-conversion.mddocs/

Date Conversion

Functions for converting between date types, calendar systems, and timezones with proper handling of ambiguous time situations.

Capabilities

Type Conversion Functions

Convert between different date and time object types while preserving appropriate information.

/**
 * Extracts date components from date/time objects
 * @param dateTime - Any object with date components
 * @returns CalendarDate with only date information
 */
function toCalendarDate(dateTime: AnyCalendarDate): CalendarDate;

/**
 * Converts to CalendarDateTime with optional time
 * @param date - Date object to convert
 * @param time - Optional time to combine with date
 * @returns CalendarDateTime object
 */
function toCalendarDateTime(date: CalendarDate | CalendarDateTime | ZonedDateTime, time?: AnyTime): CalendarDateTime;

/**
 * Extracts time components from datetime objects
 * @param dateTime - Object with time components
 * @returns Time object with only time information
 */
function toTime(dateTime: CalendarDateTime | ZonedDateTime): Time;

Usage Examples:

import { 
  toCalendarDate, 
  toCalendarDateTime, 
  toTime,
  ZonedDateTime,
  Time
} from "@internationalized/date";

const zonedDT = new ZonedDateTime(2024, 3, 15, 'UTC', 0, 14, 30);

// Extract date part
const dateOnly = toCalendarDate(zonedDT); // CalendarDate: 2024-03-15

// Convert to CalendarDateTime
const dateTime = toCalendarDateTime(dateOnly, new Time(9, 30));

// Extract time part
const timeOnly = toTime(zonedDT); // Time: 14:30:00

Calendar System Conversion

Convert dates between different calendar systems while maintaining the same absolute point in time.

/**
 * Converts date between calendar systems
 * @param date - Date in any calendar system
 * @param calendar - Target calendar system
 * @returns Date converted to target calendar system
 */
function toCalendar<T extends AnyCalendarDate>(date: T, calendar: Calendar): T;

Usage Examples:

import { 
  toCalendar, 
  CalendarDate, 
  createCalendar,
  GregorianCalendar 
} from "@internationalized/date";

const gregorianDate = new CalendarDate(2024, 3, 15);
const buddhist = createCalendar('buddhist');
const persian = createCalendar('persian');

// Convert to Buddhist calendar (BE 2567)
const buddhistDate = toCalendar(gregorianDate, buddhist);

// Convert to Persian calendar  
const persianDate = toCalendar(gregorianDate, persian);

// Convert back to Gregorian
const backToGregorian = toCalendar(buddhistDate, new GregorianCalendar());

Timezone Conversion Functions

Convert between timezones and handle timezone-aware operations with DST disambiguation.

/**
 * Converts to ZonedDateTime in specified timezone
 * @param date - Date to convert
 * @param timeZone - Target IANA timezone identifier
 * @param disambiguation - How to handle ambiguous times during DST transitions
 * @returns ZonedDateTime in specified timezone
 */
function toZoned(
  date: CalendarDate | CalendarDateTime | ZonedDateTime, 
  timeZone: string, 
  disambiguation?: Disambiguation
): ZonedDateTime;

/**
 * Converts ZonedDateTime between timezones
 * @param date - ZonedDateTime to convert
 * @param timeZone - Target IANA timezone identifier
 * @returns ZonedDateTime in new timezone representing same instant
 */
function toTimeZone(date: ZonedDateTime, timeZone: string): ZonedDateTime;

/**
 * Converts to user's local timezone
 * @param date - ZonedDateTime to convert
 * @returns ZonedDateTime in local timezone
 */
function toLocalTimeZone(date: ZonedDateTime): ZonedDateTime;

type Disambiguation = 'compatible' | 'earlier' | 'later' | 'reject';

Usage Examples:

import { 
  toZoned, 
  toTimeZone, 
  toLocalTimeZone,
  CalendarDateTime 
} from "@internationalized/date";

const dateTime = new CalendarDateTime(2024, 3, 15, 14, 30);

// Convert to specific timezone
const nyTime = toZoned(dateTime, 'America/New_York');
const tokyoTime = toZoned(dateTime, 'Asia/Tokyo');

// Handle DST ambiguity during "fall back"
const ambiguousTime = new CalendarDateTime(2024, 11, 3, 1, 30); // DST ends
const earlierTime = toZoned(ambiguousTime, 'America/New_York', 'earlier');
const laterTime = toZoned(ambiguousTime, 'America/New_York', 'later');

// Convert between timezones (same instant, different representation)
const parisTime = toTimeZone(nyTime, 'Europe/Paris');

// Convert to user's local timezone
const localTime = toLocalTimeZone(nyTime);

JavaScript Date Conversion

Convert between the library's date objects and native JavaScript Date objects.

/**
 * Converts JavaScript Date to ZonedDateTime
 * @param date - JavaScript Date object
 * @param timeZone - IANA timezone identifier for interpretation
 * @returns ZonedDateTime representing the same instant
 */
function fromDate(date: Date, timeZone: string): ZonedDateTime;

/**
 * Converts Unix timestamp to ZonedDateTime
 * @param ms - Unix timestamp in milliseconds
 * @param timeZone - IANA timezone identifier for representation
 * @returns ZonedDateTime representing the timestamp
 */
function fromAbsolute(ms: number, timeZone: string): ZonedDateTime;

Usage Examples:

import { fromDate, fromAbsolute } from "@internationalized/date";

// Convert JavaScript Date
const jsDate = new Date('2024-03-15T14:30:00Z');
const zonedFromJS = fromDate(jsDate, 'America/New_York');

// Convert Unix timestamp
const timestamp = Date.now();
const zonedFromTimestamp = fromAbsolute(timestamp, 'Europe/London');

// Use with different timezones
const utcTime = fromAbsolute(timestamp, 'UTC');
const localTime = fromAbsolute(timestamp, 'America/Los_Angeles');

Advanced Conversion Scenarios

Handle complex conversion scenarios with multiple steps and edge cases.

Converting Between Calendar Systems and Timezones:

import { 
  CalendarDate, 
  createCalendar, 
  toCalendar, 
  toZoned,
  toTimeZone 
} from "@internationalized/date";

// Start with Persian date
const persianDate = new CalendarDate(createCalendar('persian'), 1403, 1, 26);

// Convert to Gregorian calendar
const gregorianDate = toCalendar(persianDate, createCalendar('gregory'));

// Add time and timezone
const zonedDT = toZoned(gregorianDate.set({ hour: 14, minute: 30 }), 'Asia/Tehran');

// Convert to different timezone
const nyTime = toTimeZone(zonedDT, 'America/New_York');

DST Transition Handling:

import { CalendarDateTime, toZoned } from "@internationalized/date";

// Spring forward - 2:30 AM doesn't exist
const springForward = new CalendarDateTime(2024, 3, 10, 2, 30);

// Different disambiguation strategies
const compatible = toZoned(springForward, 'America/New_York', 'compatible'); // 3:30 AM
const earlier = toZoned(springForward, 'America/New_York', 'earlier'); // 1:30 AM  
const later = toZoned(springForward, 'America/New_York', 'later'); // 3:30 AM

// Fall back - 1:30 AM happens twice
const fallBack = new CalendarDateTime(2024, 11, 3, 1, 30);
const firstOccurrence = toZoned(fallBack, 'America/New_York', 'earlier'); // First 1:30 AM
const secondOccurrence = toZoned(fallBack, 'America/New_York', 'later'); // Second 1:30 AM

Types

/**
 * Disambiguation strategy for handling ambiguous times during DST transitions
 */
type Disambiguation = 'compatible' | 'earlier' | 'later' | 'reject';

/**
 * Interface compatible with any object with date fields
 */
interface AnyCalendarDate {
  readonly calendar: Calendar;
  readonly era: string;
  readonly year: number;
  readonly month: number;
  readonly day: number;
  copy(): this;
}

/**
 * Interface compatible with any object with time fields
 */
interface AnyTime {
  readonly hour: number;
  readonly minute: number;
  readonly second: number;
  readonly millisecond: number;
  copy(): this;
}

docs

calendar-systems.md

date-conversion.md

date-formatting.md

date-queries.md

date-time-objects.md

index.md

string-parsing.md

tile.json