or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

arithmetic.mdcomponents.mdconstants.mdformatting.mdfp.mdi18n.mdindex.mdparsing.mdperiods.mdvalidation.md
tile.json

parsing.mddocs/

Date Parsing

Date parsing functions provide flexible conversion from strings to Date objects with support for various formats, ISO standards, and custom patterns. All parsing functions include proper error handling and validation.

Core Parsing

parse

Parse a date from a string using a format pattern.

function parse(
  dateString: string,
  formatString: string,
  referenceDate: DateArg<Date>,
  options?: ParseOptions
): Date;

Parameters:

  • dateString - The string to parse
  • formatString - The format pattern to match
  • referenceDate - Reference date for relative parsing
  • options - Optional parsing configuration

Format Tokens:

  • yyyy - 4-digit year
  • MM - 2-digit month (01-12)
  • dd - 2-digit day (01-31)
  • HH - 24-hour format hour (00-23)
  • mm - 2-digit minute (00-59)
  • ss - 2-digit second (00-59)
  • SSS - Milliseconds (000-999)

Examples:

import { parse } from "date-fns";

// Basic date parsing
parse('2014-02-11', 'yyyy-MM-dd', new Date());
//=> Tue Feb 11 2014 00:00:00

// Date and time
parse('2014-02-11 14:30:45', 'yyyy-MM-dd HH:mm:ss', new Date());
//=> Tue Feb 11 2014 14:30:45

// Custom format
parse('11.02.2014', 'dd.MM.yyyy', new Date());
//=> Tue Feb 11 2014 00:00:00

// With milliseconds
parse('2014-02-11T14:30:45.123', 'yyyy-MM-ddTHH:mm:ss.SSS', new Date());
//=> Tue Feb 11 2014 14:30:45.123

parseISO

Parse an ISO 8601 date string.

function parseISO<ResultDate extends Date = Date>(
  argument: string,
  options?: ParseISOOptions<ResultDate>
): ResultDate;

Parameters:

  • argument - The ISO 8601 string to parse
  • options - Optional parsing configuration with additional digits support

Supported Formats:

  • YYYY-MM-DD - Calendar date
  • YYYY-MM-DDTHH:mm:ss - Complete date and time
  • YYYY-MM-DDTHH:mm:ss.sss - With milliseconds
  • YYYY-MM-DDTHH:mm:ssZ - UTC time
  • YYYY-MM-DDTHH:mm:ss+HH:mm - With timezone offset

Examples:

import { parseISO } from "date-fns";

// Date only
parseISO('2014-02-11');
//=> Tue Feb 11 2014 00:00:00

// Complete datetime
parseISO('2014-02-11T11:30:30');
//=> Tue Feb 11 2014 11:30:30

// With timezone
parseISO('2014-02-11T11:30:30+05:00');
//=> Tue Feb 11 2014 11:30:30

// UTC time
parseISO('2014-02-11T11:30:30Z');
//=> Tue Feb 11 2014 11:30:30

// With milliseconds
parseISO('2014-02-11T11:30:30.123Z');
//=> Tue Feb 11 2014 11:30:30.123

parseJSON

Parse a date from JSON (handles various JSON date formats).

function parseJSON<ResultDate extends Date = Date>(
  dateStr: string,
  options?: ParseJSONOptions<ResultDate>
): ResultDate;

Parameters:

  • dateStr - The JSON date string to parse
  • options - Optional parsing configuration with context support

Examples:

import { parseJSON } from "date-fns";

// ISO string from JSON
parseJSON('2014-02-11T11:30:30.000Z');
//=> Tue Feb 11 2014 11:30:30

// Unix timestamp
parseJSON(1392123030000);
//=> Tue Feb 11 2014 11:30:30

// Already a Date object
parseJSON(new Date(2014, 1, 11));
//=> Tue Feb 11 2014 00:00:00

Validation and Matching

isMatch

Check if a string matches a date format pattern.

function isMatch(
  dateString: string,
  formatString: string,
  options?: MatchOptions
): boolean;

Parameters:

  • dateString - The string to test
  • formatString - The format pattern to match against
  • options - Optional matching configuration

Examples:

import { isMatch } from "date-fns";

// Valid formats
isMatch('2014-02-11', 'yyyy-MM-dd');
//=> true

isMatch('11.02.2014', 'dd.MM.yyyy');
//=> true

// Invalid formats
isMatch('2014-02-11', 'dd.MM.yyyy');
//=> false

isMatch('not-a-date', 'yyyy-MM-dd');
//=> false

// Complex patterns
isMatch('2014-02-11 14:30:45', 'yyyy-MM-dd HH:mm:ss');
//=> true

Advanced Parsing Patterns

Date Parts Parsing

Parse dates with various separators and formats:

import { parse } from "date-fns";

// Different separators
parse('2014/02/11', 'yyyy/MM/dd', new Date());
parse('2014-02-11', 'yyyy-MM-dd', new Date());
parse('2014.02.11', 'yyyy.MM.dd', new Date());

// Different order
parse('11/02/2014', 'dd/MM/yyyy', new Date());
parse('02/11/2014', 'MM/dd/yyyy', new Date());

// Short year
parse('14-02-11', 'yy-MM-dd', new Date());

Time Parsing

Parse various time formats:

import { parse } from "date-fns";

// 24-hour format
parse('14:30:45', 'HH:mm:ss', new Date());

// 12-hour format with AM/PM
parse('2:30:45 PM', 'h:mm:ss a', new Date());

// Minutes and seconds only
parse('30:45', 'mm:ss', new Date());

// With milliseconds
parse('14:30:45.123', 'HH:mm:ss.SSS', new Date());

Relative Date Parsing

Use reference date for context-dependent parsing:

import { parse } from "date-fns";

const referenceDate = new Date(2020, 0, 1); // Jan 1, 2020

// Parse relative to reference year
parse('02-11', 'MM-dd', referenceDate);
//=> Feb 11, 2020

// Day of year
parse('42', 'D', referenceDate);
//=> Feb 11, 2020 (42nd day of 2020)

Error Handling

Invalid Date Detection

import { parse, isValid } from "date-fns";

// Parse potentially invalid date
const result = parse('invalid-date', 'yyyy-MM-dd', new Date());

// Check if parsing was successful
if (isValid(result)) {
  console.log('Parsed successfully:', result);
} else {
  console.log('Parsing failed');
}

// Parse with validation
function safeParse(dateString: string, format: string): Date | null {
  try {
    const parsed = parse(dateString, format, new Date());
    return isValid(parsed) ? parsed : null;
  } catch {
    return null;
  }
}

Common Parsing Pitfalls

import { parse, parseISO } from "date-fns";

// Ambiguous formats - be explicit
parse('01/02/2014', 'MM/dd/yyyy', new Date()); // Jan 2, 2014
parse('01/02/2014', 'dd/MM/yyyy', new Date()); // Feb 1, 2014

// Timezone handling
parseISO('2014-02-11T11:30:30'); // Local time
parseISO('2014-02-11T11:30:30Z'); // UTC time

// Invalid dates return Invalid Date
parse('2014-13-01', 'yyyy-MM-dd', new Date()); // Invalid Date
parse('2014-02-30', 'yyyy-MM-dd', new Date()); // Invalid Date

Option Types

ParseOptions

interface ParseOptions {
  locale?: Locale;
  weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
  firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7;
  useAdditionalWeekYearTokens?: boolean;
  useAdditionalDayOfYearTokens?: boolean;
}

MatchOptions

interface MatchOptions {
  locale?: Locale;
  weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
  firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7;
  useAdditionalWeekYearTokens?: boolean;
  useAdditionalDayOfYearTokens?: boolean;
}

ParseISOOptions

interface ParseISOOptions<DateType extends Date = Date> extends ContextOptions<DateType> {
  additionalDigits?: 0 | 1 | 2;
}

ParseJSONOptions

interface ParseJSONOptions<DateType extends Date = Date> extends ContextOptions<DateType> {}

Format Token Reference

Date Tokens

TokenDescriptionExample
yyyy4-digit year2014
yy2-digit year14
yYear2014
YYYYISO week year2014
YY2-digit ISO week year14

Month Tokens

TokenDescriptionExample
MMMMFull month nameFebruary
MMMShort month nameFeb
MM2-digit month02
MMonth2

Day Tokens

TokenDescriptionExample
dd2-digit day11
dDay11
DDay of year42
EEEEFull day nameTuesday
EEEShort day nameTue
eLocal day of week2
iISO day of week2

Time Tokens

TokenDescriptionExample
HH24-hour hour14
H24-hour hour14
hh12-hour hour02
h12-hour hour2
mm2-digit minute30
mMinute30
ss2-digit second45
sSecond45
SSSMillisecond123
S1/10 second1
SS1/100 second12

AM/PM and Timezone

TokenDescriptionExample
aAM/PMPM
aaAM/PMPM
aaaAM/PMPM
XTimezone offset+0200
XXTimezone offset+02:00
XXXTimezone offset+02:00