Implementation:Apache Druid Date Parsing And Timezone Conversion
| Knowledge Sources | |
|---|---|
| Domains | Web_Console, Date_Time_Utilities |
| Last Updated | 2026-02-10 10:00 GMT |
Overview
Provides date parsing, formatting, and timezone-aware conversion utilities for the Druid web console.
Description
The Date Utils module supplies a comprehensive set of functions for working with ISO 8601 dates, UTC-to-local conversions, date range manipulation, and interval parsing. It integrates with Blueprint.js DateRange types, the chronoshift Timezone type, and the dayjs library. A configurable `formatDate` function reads user preferences from localStorage to determine whether dates should display in local time or UTC.
Usage
Used throughout the web console wherever dates need to be parsed from strings, formatted for display, converted between UTC and local time, or transformed between Druid interval strings and JavaScript DateRange objects.
Code Reference
Source Location
- Repository: Apache Druid
- File: web-console/src/utils/date.ts
- Lines: 1-186
Signature
export const DATE_FORMAT = 'YYYY-MM-DDTHH:mm:ss.SSSZ';
export function isNonNullRange(range: DateRange): range is NonNullDateRange;
export function dateToIsoDateString(date: Date): string;
export function prettyFormatIsoDateWithMsIfNeeded(isoDate: string | Date): string;
export function prettyFormatIsoDate(isoDate: string | Date): string;
export function toIsoStringInTimezone(date: Date, timezone: Timezone): string;
export function utcToLocalDate(utcDate: Date): Date;
export function localToUtcDate(localDate: Date): Date;
export function utcToLocalDateRange([start, end]: DateRange): DateRange;
export function localToUtcDateRange([start, end]: DateRange): DateRange;
export function intervalToLocalDateRange(interval: string): DateRange;
export function localDateRangeToInterval(localRange: DateRange): string;
export function maxDate(a: Date, b: Date): Date;
export function minDate(a: Date, b: Date): Date;
export function formatDate(value: string): string;
export function parseIsoDate(dateString: string): Date;
Import
import { dateToIsoDateString, parseIsoDate, formatDate, utcToLocalDate } from './utils/date';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| date | Date |
Yes | A JavaScript Date object for conversion or formatting |
| isoDate | Date | Yes | An ISO 8601 date string or Date object for pretty-printing |
| dateString | string |
Yes | An ISO 8601 date string to parse (flexible format from year-only to full datetime) |
| timezone | Timezone |
Yes | A chronoshift Timezone for timezone-aware conversions |
| interval | string |
Yes | A Druid-style interval string (start/end separated by /) |
| range | DateRange |
Yes | null, Date | null] |
Outputs
| Name | Type | Description |
|---|---|---|
| dateString | string |
Formatted or converted date as a string |
| dateRange | DateRange |
A pair of nullable Date objects representing a range |
| date | Date |
Parsed or converted JavaScript Date object |
| boolean | boolean |
Type guard result for non-null range checks |
Usage Examples
Parsing a flexible ISO date
import { parseIsoDate } from './utils/date';
const fullDate = parseIsoDate('2016-06-20T21:31:02.123');
const yearOnly = parseIsoDate('2016'); // defaults to Jan 1 00:00:00.000
Converting between UTC and local time
import { utcToLocalDate, localToUtcDate } from './utils/date';
const localDate = utcToLocalDate(new Date('2024-01-15T12:00:00Z'));
const utcDate = localToUtcDate(localDate);
Formatting a date with user preferences
import { formatDate } from './utils/date';
// Returns local time or ISO string based on localStorage config
const formatted = formatDate('2024-01-15T12:00:00.000Z');