Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api/isValid.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ isValid('2025-02-30', 'YYYY-MM-DD'); // => false (Feb 30th doesn't exist)
isValid('2025-13-01', 'YYYY-MM-DD'); // => false (month 13 doesn't exist)
isValid('25:30:00', 'HH:mm:ss'); // => false (hour 25 doesn't exist)
isValid('invalid-date', 'YYYY-MM-DD'); // => false (doesn't match format)
isValid('2025-08', 'YYYY-MM-DD'); // => false (input ends before the day)

// Format mismatch
isValid('2025-08-23', 'MM/DD/YYYY'); // => false (wrong format)
Expand Down
15 changes: 15 additions & 0 deletions docs/api/parse.md
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,16 @@ parse('2025', 'YYYY');

To customize these defaults, use the [`defaultDate`](#defaultdate) option.

Defaults apply only to components that the format string does not contain. Every token in the format string must match the input, so input that ends early fails instead of being filled in:

```typescript
import { parse } from 'date-and-time';

// The format string expects a day, but the input ends after the month
parse('2025-08', 'YYYY-MM-DD');
// => Invalid Date
```

### Date Range Limitations

The parsable maximum date is `December 31, 9999`, and the minimum date is `January 1, 0001`.
Expand Down Expand Up @@ -516,6 +526,10 @@ parse('2025/08/23 14:30:45', 'YYYY/MM/DD...');
// More complex example
parse('Log entry: 2025-08-23 some extra data here', '[Log entry: ]YYYY-MM-DD...');
// => Sat Aug 23 2025 00:00:00 GMT-0700

// Tokens before the ellipsis must still match
parse('2025/08', 'YYYY/MM/DD...');
// => Invalid Date
```

### Complex Localized Parsing
Expand Down Expand Up @@ -576,6 +590,7 @@ parse('2025-02-30', 'YYYY-MM-DD'); // Invalid - Feb 30th doesn't exist
parse('2025-13-01', 'YYYY-MM-DD'); // Invalid - month 13 doesn't exist
parse('25:30:00', 'HH:mm:ss'); // Invalid - hour 25 doesn't exist
parse('12 hours 34', 'HH hours mm'); // Invalid - format mismatch
parse('2025-08', 'YYYY-MM-DD'); // Invalid - input ends before the day
```

## Performance Considerations
Expand Down
36 changes: 23 additions & 13 deletions docs/api/preparse.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ interface ParseResult {
_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
}
```

Expand Down Expand Up @@ -106,12 +107,12 @@ import { preparse, parse, isValid } from 'date-and-time';
function safeParse(dateString: string, format: string) {
const result = preparse(dateString, format);

// Check if required components are present
if (result.Y && result.M && result.D) {
// 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('Missing required date components');
throw new Error('Invalid date or missing required date components');
}
```

Expand All @@ -121,7 +122,7 @@ function safeParse(dateString: string, format: string) {
function validateBusinessDay(dateString: string, format: string) {
const result = preparse(dateString, format);

if (result.Y && result.M && result.D) {
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();

Expand All @@ -138,28 +139,37 @@ function validateBusinessDay(dateString: string, format: string) {

## Error Handling

If parsing fails, `preparse()` returns an object with parsing metadata but without the main date components (Y, M, D, H, etc.).
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.

```typescript
const result = preparse('invalid-date', 'YYYY-MM-DD');
console.log(result);
// {
// _index: 0,
// _length: 12,
// _match: 0
// _match: 0,
// _mismatch: true
// }

// Check for successful parsing by looking for date components
if (!result.Y && !result.M && !result.D) {
console.log('Parsing failed - no date components found');
}
// 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
// }

// Alternative: check the _match property
if (result._match === 0) {
console.log('Parsing failed - no tokens matched');
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()`](./isValid) to check both.

## See Also

- [`parse()`](./parse) - Parse formatted date strings into Date objects
Expand Down
13 changes: 13 additions & 0 deletions docs/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ parse(
// => Jan 02 2015 23:14:05 GMT+0100
```

The input must also match the whole format string. v3 accepted input that ended before the format string did and filled in the missing components with their default values, so `date.parse('2025-08', 'YYYY-MM-DD')` returned August 1, 2025. v4 returns `Invalid Date` for such input. Parse it with a format string that matches it instead:

```typescript
import { parse } from 'date-and-time';

parse('2025-08', 'YYYY-MM-DD');
// => Invalid Date

parse('2025-08', 'YYYY-MM');
// => Aug 01 2025 00:00:00 GMT-0700
```

### preparse

The third argument has been changed from `boolean` to `ParserOptions`. With `ParserOptions`, you can now specify timezone and locale settings. If you previously set the third argument to `true` to parse input in UTC timezone, you can achieve the same output as follows:
Expand All @@ -122,6 +134,7 @@ Additionally, the `PreparseResult` object returned by the `preparse` function ha

- Properties for tokens that were not read are not included. For example, in the code example above, since the string does not contain a date part, properties representing dates such as `Y`, `M`, and `D` are not included.
- Read values are returned as-is. For example, previously, time read in 12-hour format was converted to 24-hour format, but this is no longer done.
- When the input does not match the whole format string, the result has `_mismatch: true`. Properties read before the mismatch are still included.

### isValid

Expand Down
Loading
Loading