export class DateParser2 {
    static tryParse(value, format, locale) {
        try {
            return this.parse(value, format, locale);
        }
        catch {
            return null;
        }
    }
    static parse(value, format, locale) {
        const segments = format.match(/d{1,4}|y{1,4}|M{1,4}|[-/. ]+/g);
        if (!segments) {
            throw new Error(`Unable to parse format string: "${format}".`);
        }
        const parsedSegments = segments.map((segment) => {
            if ((/[-/. ]+/).test(segment)) {
                return segment; // Return separator as-is
            }
            return this.parseSegment(value, segment);
        });
        const parsedValue = parsedSegments.join('');
        return new Date(Date.parse(parsedValue));
    }
    static parseSegment(value, segment) {
        const segmentLength = segment.length;
        const segmentValue = value.substring(0, segmentLength);
        if (segmentValue.length !== segmentLength) {
            throw new Error(`Unable to parse segment: "${segment}".`);
        }
        return segmentValue;
    }
}
//# sourceMappingURL=DateParser2.js.map