Overview
The Custom Parse Format plugin adds the ability to parse date strings using custom format patterns. This is similar to Day.js’scustomParseFormat plugin and supports a wide variety of format tokens.
Installation
Load the plugin using eitherextend or lazyLoad:
Methods
atemporal.fromFormat(dateString, formatString, timeZone?)
Creates an Atemporal instance from a string and a specific format.
Signature:
dateString: The date string to parseformatString: The format that the date string follows (e.g., ‘YYYY-MM-DD’)timeZone(optional): The timezone to apply. If not provided, uses the default
TemporalWrapper instance
Examples:
Format Tokens
The plugin supports a comprehensive set of format tokens:Year
| Token | Description | Example |
|---|---|---|
YYYY | 4-digit year | 2023 |
YY | 2-digit year | 23 (interpreted as 2023) |
- Years
00-68→2000-2068 - Years
69-99→1969-1999
Month
| Token | Description | Example |
|---|---|---|
MM | Month with leading zero | 01, 12 |
M | Month without leading zero | 1, 12 |
MMMM | Full month name | January, December |
MMM | Abbreviated month name | Jan, Dec |
Day
| Token | Description | Example |
|---|---|---|
DD | Day with leading zero | 01, 31 |
D | Day without leading zero | 1, 31 |
DDD | Day of year (3 digits) | 001, 365 |
DDDD | Day of year (1-3 digits) | 1, 365 |
Hour
| Token | Description | Example |
|---|---|---|
HH | Hour (24-hour, leading zero) | 00, 23 |
H | Hour (24-hour, no leading zero) | 0, 23 |
hh | Hour (12-hour, leading zero) | 01, 12 |
h | Hour (12-hour, no leading zero) | 1, 12 |
Minute
| Token | Description | Example |
|---|---|---|
mm | Minute with leading zero | 00, 59 |
m | Minute without leading zero | 0, 59 |
Second
| Token | Description | Example |
|---|---|---|
ss | Second with leading zero | 00, 59 |
s | Second without leading zero | 0, 59 |
Millisecond
| Token | Description | Example |
|---|---|---|
SSS | Milliseconds (3 digits) | 000, 999 |
SS | Centiseconds (2 digits) | 00, 99 |
S | Deciseconds (1 digit) | 0, 9 |
AM/PM
| Token | Description | Example |
|---|---|---|
A | AM/PM uppercase | AM, PM |
a | am/pm lowercase | am, pm |
h or hh), an AM/PM token is required.
Validation
The plugin includes comprehensive validation:- Range validation: Ensures values are within valid ranges
- Date validation: Uses Temporal API to validate dates (e.g., rejects February 30th)
- Leap year support: Correctly handles leap years
- Month-specific days: Validates day counts for each month
- 12-hour format: Requires AM/PM marker when using
horhh
Strict Mode
The parsing is strict by default. The entire string must match the format pattern exactly:Flexible Whitespace
The parser allows flexible whitespace matching. Single spaces in the format string will match any amount of whitespace:Literal Text
You can include literal text in format strings using square brackets:Examples
Common Date Formats
Time Formats
Combined Date and Time
Day of Year
With Timezone
Error Handling
Performance
The plugin includes an LRU cache (max 100 entries) that stores compiled regular expressions for format strings. This significantly improves performance when parsing multiple dates with the same format.Cache Management
Best Practices
- Reuse format strings: The cache makes repeated parsing with the same format very fast
- Use strict formats: Always match the exact format of your input strings
- Validate results: Check
.isValid()after parsing untrusted input - Specify timezones: Always provide a timezone when parsing times from specific locations
- Prefer ISO format: When possible, use ISO 8601 format strings for best compatibility