Blazing fast & lightweight (173 bytes) date formatting for Node.js and the browser.
Inspired by tinydate
, tinytime
, date-fns
and more, this module aims to provide super fast and easy way to format dates, while also staying lightweight.
Note that only a limited subset of Date methods is provided.
- Small. 173 bytes (minified and gzipped). No dependencies. Size Limit controls the size.
- Fast. See the benchmarks.
- Compliant. Follows Unicode Technical Standard #35.
- Well tested. To make sure it handles various use cases correctly.
- Portable. Works pretty much everywhere.
- Written in TypeScript.
$ npm install light-date
import {format} from 'light-date';
const date = new Date('5/1/2020, 4:30:09 PM');
format(date, 'The date is {MM}/{dd}/{yyyy}!'); //=> 'The date is 05/01/2020!'
Returns a string with formatted date.
Type: Date
Date object, which should be used.
Type: string
String, which you want to format, for example: {yyyy}-{MM}-{dd}
or Current time: {hh}:{mm}:{ss}
.
Format of the string is based on Unicode Technical Standard #35.
Unit | Pattern | Result examples |
---|---|---|
Calendar year | {yy} | 44, 01, 00, 17 |
{yyyy} | 0044, 0001, 1900, 2020 | |
Month | {MM} | 01, 02, ..., 12 |
Day | {dd} | 01, 02, ..., 31 |
Hour | {HH} | 00, 01, 02, ..., 23 |
Minute | {mm} | 00, 01, ..., 59 |
Second | {ss} | 00, 01, ..., 59 |
Millisecond | {SSS} | 000, 0001, ..., 999 |
# Node.js v12.18.3
light-date x 7,382,656 ops/sec ±0.94% (90 runs sampled)
tinytime x 5,283,247 ops/sec ±0.57% (94 runs sampled)
tinydate x 4,002,904 ops/sec ±0.43% (92 runs sampled)
date-format x 830,962 ops/sec ±0.14% (97 runs sampled)
moment x 674,096 ops/sec ±0.47% (94 runs sampled)
date-fns lightFormat x 462,305 ops/sec ±0.19% (97 runs sampled)
date-fns format x 345,092 ops/sec ±0.19% (98 runs sampled)
dayjs x 262,442 ops/sec ±0.28% (94 runs sampled)
MIT © Antoni Kepinski