A lightweight, zero‑dependency string‑to‑string date format converter with pluggable tokens and an extensible architecture.
Convert date strings from one format to another using pure token-to-token logic—no internal Date
objects and no assumptions. You control everything via inputs.
- Fast: Parses and formats via pre‑compiled templates
- Pluggable Tokens: Add or override tokens without touching core code
- Flexible Year Handling: Supply a
yearConverter
to expand two‑digit years or provide defaults - Override & Default Tokens: Control individual token output on a per-call basis
- Error Policies: Choose strict (throw) or silent (best‑effort) behavior
- Bracketed Literals: Use
[text]
to preserve literals in format output - Zero Dependencies: Core library is pure JS—no external packages
npm install datefmt-lite
# or
yarn add datefmt-lite
import { formatDate } from 'datefmt-lite';
formatDate('20250425', 'yyyyMMdd', 'dd/MM/yyyy');
// → '25/04/2025'
formatDate('250425', 'yyMMdd', 'dd/MM/yyyy', {
yearConverter: (yy) => 2000 + yy,
});
// → '25/04/2025'
- You control both the input and output formats
- You want predictable, fast token-to-token formatting
- You want fallback behavior instead of runtime errors
- You don’t need built-in date math, timezone offsets, or localization (but you can inject that logic via tokens)
✅ Perfect for:
- ETL data pipelines
- Formatted export tools
- Browser-safe string conversion
- Small-bundle apps where you want full control
Token | Meaning | Example |
---|---|---|
yyyy | full year | 2025 |
yy | 2-digit year | 25 |
MMMM | full month | April |
MMM | short month | Apr |
MM | 2-digit month | 04 |
M | 1/2-digit month | 4 |
dd | 2-digit day | 09 |
d | 1/2-digit day | 9 |
HH | 2-digit hour | 03 |
H | 1/2-digit hour | 3 |
mm | 2-digit minute | 07 |
m | 1/2-digit minute | 7 |
ss | 2-digit second | 09 |
s | 1/2-digit second | 9 |
➡️ Token definitions follow Unicode Date Field Symbols.
➡️ See docs/tokens.md
for the full list and examples.
formatDate(inputDate, inputFormat, outputFormat, {
errorPolicy: 'silent',
yearConverter: (yy) => 1900 + yy,
customTokens: {
Q: (p) => 'Q' + Math.ceil(p.month / 3),
},
overrideTokens: {
dd: '01',
},
defaultTokens: {
MM: '00',
},
});
'throw'
(default): throws on parse/validation errors'silent'
: returns raw input if nothing parsed; otherwise returns best-effort output with literal token fallback
➡️ See docs/formatting-behavior.md
- Required when using
yy
(2-digit year) - If not provided in silent mode, raw
yy
is used as-is ('25' → 0025
)
- Add your own tokens
customTokens: {
Q: (p) => 'Q' + Math.ceil(p.month / 3)
}
- Override built-in or derived tokens (e.g., force day = '01')
- Provide fallback values when parsed data is missing
# Run tests
yarn test
# Build ESM and CJS
yarn build
See /docs
for:
api.md
: MainformatDate
function signature, options, and behaviortokens.md
: List of supported tokens (e.g.,yyyy
,dd
,MMM
,HH
) and how they're matchedexamples.md
: Real-world and edge-case formatting examplesformatting-behavior.md
: HowoverrideTokens
,defaultTokens
, anderrorPolicy
interactinternals.md
: Behind-the-scenes architecture and function flow for contributorscontributing.md
: Development setup, code structure, and how to contributedesign.md
: Core philosophy, extensibility plan, and long-term goals
See CHANGELOG.md for release history, breaking changes, and upgrade notes.
MIT © 2025