Skip to content

preparse()

Pre-parses date strings and returns intermediate parsing results. This function is useful when you need to examine parsing details before creating a final Date object.

Syntax

preparse(dateString, formatString[, options])

Parameters

Parameter Type Required Description
dateString string Yes The date string to parse
formatString string | CompiledObject Yes Format pattern string or compiled object
options ParserOptions No Parsing options for customization (see parse() for details)

Returns

PreparseResult - Object containing parsed date components

interface PreparseResult {
Y?: number // Year
M?: number // Month (1-12)
D?: number // Day
H?: number // Hours (24-hour format, 0-23)
A?: number // AM/PM (0 = AM, 1 = PM)
h?: number // Hours (12-hour format, 1-12)
m?: number // Minutes (0-59)
s?: number // Seconds (0-59)
S?: number // Milliseconds (0-999)
Z?: number // Timezone offset in minutes
_index?: number // Current parsing position
_length?: number // Total length of input string
_match?: number // Number of matched tokens
_mismatch?: true // Set when the input did not match the whole format string
}

Basic Examples

Simple Date Parsing

import { preparse } from 'date-and-time';
const result = preparse('2025-08-23 14:30:45', 'YYYY-MM-DD HH:mm:ss');
console.log(result);
// {
// _index: 19,
// _length: 19,
// _match: 6,
// Y: 2025,
// M: 8,
// D: 23,
// H: 14,
// m: 30,
// s: 45
// }

With 12-hour Format

const result = preparse('2025-08-23 2:30:45 PM', 'YYYY-MM-DD h:mm:ss A');
console.log(result);
// {
// _index: 21,
// _length: 21,
// _match: 7,
// Y: 2025,
// M: 8,
// D: 23,
// h: 2,
// m: 30,
// s: 45,
// A: 1 // PM
// }

With Localized Content

import { preparse } from 'date-and-time';
import ja from 'date-and-time/locales/ja';
const result = preparse('2025年8月23日', 'YYYY年M月D日', { locale: ja });
console.log(result);
// {
// _index: 10,
// _length: 10,
// _match: 3,
// Y: 2025,
// M: 8,
// D: 23
// }

ParserOptions

The options parameter accepts the same ParserOptions as the parse() function. This includes locale, timezone, numeral system, calendar, case sensitivity, hour format settings, and defaultDate. Refer to the parse documentation for complete details on all available options.

Use Cases

Validation Before Parsing

import { preparse, parse, isValid } from 'date-and-time';
function safeParse(dateString: string, format: string) {
const result = preparse(dateString, format);
// Check that the input is valid and contains a year, month, and day
if (isValid(dateString, format) && result.Y && result.M && result.D) {
return parse(dateString, format);
}
throw new Error('Invalid date or missing required date components');
}

Custom Date Validation

function validateBusinessDay(dateString: string, format: string) {
const result = preparse(dateString, format);
if (isValid(dateString, format) && result.Y && result.M && result.D) {
const date = new Date(result.Y, result.M - 1, result.D);
const dayOfWeek = date.getDay();
if (dayOfWeek === 0 || dayOfWeek === 6) {
throw new Error('Business days only (Monday-Friday)');
}
return date;
}
throw new Error('Invalid date format');
}

Error Handling

If the input does not match the whole format string, preparse() sets _mismatch: true on the result. Components read before the mismatch are kept, so a result can contain date components and still be a failure.

const result = preparse('invalid-date', 'YYYY-MM-DD');
console.log(result);
// {
// _index: 0,
// _length: 12,
// _match: 0,
// _mismatch: true
// }
// The input ends before the day
const partial = preparse('2025-08', 'YYYY-MM-DD');
console.log(partial);
// {
// _index: 7,
// _length: 7,
// _match: 2,
// Y: 2025,
// M: 8,
// _mismatch: true
// }
if (partial._mismatch) {
console.log('Parsing failed - the input does not match the format');
}

A result without _mismatch is not necessarily valid: preparse() does not check value ranges, such as February 30, or whether input is left over after the format string ends. Use isValid() to check both.

See Also

  • parse() - Parse formatted date strings into Date objects
  • compile() - Precompile format patterns for performance
  • isValid() - Validate date string formats