Skip to content

EDTFDateTime ​

Represents a date and time with optional timezone (Level 0). The default extended profile also accepts ISO fractional seconds and signed six-digit years, including Date.toISOString() output.

Interface ​

typescript
interface EDTFDateTime extends EDTFBase {
  type: 'DateTime';
  year: number;
  month: number;
  day: number;
  hour: number;
  minute: number;
  second: number;
  fractionalSecond?: string;
  timezone?: string;
}

Properties ​

PropertyTypeDescription
type'DateTime'Type discriminator
yearnumberYear
monthnumberMonth (1-12)
daynumberDay (1-31)
hournumberHour (0-23)
minutenumberMinute (0-59)
secondnumberSecond (0-59)
fractionalSecondstring?Written fractional digits (1–3), including trailing zeros; extended profile only
timezonestring?Timezone (e.g., 'Z', '+05:00', '-08:00')

Inherited from EDTFBase ​

PropertyTypeDescription
level0Always Level 0
edtfstringOriginal EDTF string
precisionPrecision'second'; fractionalSecond optionally refines the resolution
minDateBeginning of the written time unit
maxDateEnd of the written time unit; equal to min for three fractional digits

Usage ​

typescript
import { parse, isEDTFDateTime } from '@edtf-ts/core';

const result = parse('1985-04-12T23:20:30Z');

if (result.success && isEDTFDateTime(result.value)) {
  const dt = result.value;

  console.log(dt.year);      // 1985
  console.log(dt.month);     // 4
  console.log(dt.day);       // 12
  console.log(dt.hour);      // 23
  console.log(dt.minute);    // 20
  console.log(dt.second);    // 30
  console.log(dt.timezone);  // 'Z'
}

Timezone Formats ​

DateTime supports several timezone formats:

typescript
// UTC
parse('1985-04-12T23:20:30Z');

// Positive offset
parse('1985-04-12T23:20:30+05:00');
parse('1985-04-12T23:20:30+05:30');

// Negative offset
parse('1985-04-12T23:20:30-08:00');

// Local time (no timezone)
parse('1985-04-12T23:20:30');

Precision Levels ​

The precision property remains 'second' for compatibility. Optional fractionalSecond digits refine the bounds without adding a case to the precision union:

typescript
const result = parse('1985-04-12T23:20:30Z');
if (result.success) {
  console.log(result.value.precision);  // 'second'
}
Written secondsMillisecond bounds within that second
30000–999
30.1100–199
30.12120–129
30.123123–123
30.000000–000

Fractions survive edtf, formatISO(), FuzzyDate.toISO(), human formatting, and JSON serialization. Bounds, normalization, and comparisons use the fraction. More than three fractional digits are rejected instead of truncated. With conformance: 'strict', fractional seconds and signed six-digit datetime years return UNSUPPORTED_EXTENSION.

typescript
import { FuzzyDate } from '@edtf-ts/core';

const input = new Date('2024-02-29T12:30:00.123Z').toISOString();
const timestamp = FuzzyDate.parse(input);
console.log(timestamp.toISO()); // 2024-02-29T12:30:00.123Z
console.log(timestamp.min.getTime() === timestamp.max.getTime()); // true

Converting to JavaScript Date ​

Use the .min property:

typescript
const result = parse('1985-04-12T23:20:30Z');
if (result.success) {
  const jsDate = result.value.min;
  console.log(jsDate.toISOString());  // '1985-04-12T23:20:30.000Z'
}

Type Guard ​

typescript
import { isEDTFDateTime } from '@edtf-ts/core';

const result = parse(input);
if (result.success && isEDTFDateTime(result.value)) {
  // TypeScript knows result.value is EDTFDateTime
  console.log(result.value.hour);
}

Released under the MIT License.