@one-tree-tech/tiny-date-parser
v0.2.0
Published
Simple and tiny parser for strings to dates
Downloads
190
Maintainers
Readme
@one-tree-tech/tiny-date-parser
A lightweight and simple date parser for JavaScript and TypeScript. Converts date strings into
JavaScript Date objects with minimal overhead.
Features
- ✨ Tiny and lightweight
- 🚀 Fast date parsing
- 📦 Supports both ES Modules and CommonJS
- 📝 Full TypeScript support with type definitions
- 🧪 Well-tested with Vitest
- 🎯 Zero external dependencies
Motivation
I found it frustrating that there was no lightweight and simple date parser for JavaScript. I made the assumption that most dates fall into one of three formats: YMD, DMY, or MDY.
When parsing YMD, a separator is usually present and the year is always 4 digits. Otherwise, the parser makes minimal assumptions about the separator.
I also did not want a complete date-handling library. Often, you just need to convert user input into a Date
object.
Installation
npm install @one-tree-tech/tiny-date-parserUsage
There are two functions: parseDayFirst and parseMonthFirst. Both of them handle YMD formats as long as there is a
separator and 4-digit year. Also, it is assumed that a 2-digit month and day will be provided. Any non-numeric character
can be used as a separator.
ES Modules
import { parseDayFirst, parseMonthFirst } from '@one-tree-tech/tiny-date-parser';
const date = parseDayFirst('2024-03-18');
const date = parseDayFirst('18-03-2024');
const date = parseMonthFirst('03-18-2024');
console.log(date); // Date objectCommonJS
const { parseDayFirst, parseMonthFirst } = require('@one-tree-tech/tiny-date-parser');
const date = parseDayFirst('2024-03-18');
const date = parseDayFirst('18-03-2024');
const date = parseMonthFirst('03-18-2024');
console.log(date); // Date objectDevelopment
Prerequisites
- Node.js >= 14.0.0
- npm or yarn
Build
npm run buildThis generates:
dist/esm/- ES Module filesdist/cjs/- CommonJS filesdist/types/- TypeScript definitions
Test
npm testRun tests with UI:
npm run test:uiClean
npm run cleanLicense
MIT © Kirk Wood
